Second Function in 'if statement' not calling

 
bool Long_Confirmation()
{
   if(Long_Primary_Confirmation() == true && Long_Secondary_Confirmation() == true)
   return true;
}

Hi, I am having issues with this function above. 

For some reason the function after the 'and' operand is not called.

The 'Long_Secondary_Confirmation' function itself does work fine, it's just when in this position in an 'if statement' it fails to call.

Similarly when i switch the two functions around to have the code in the following format, the 'Long_Primary_Confirmation' function fails to call.

bool Long_Confirmation()
{
   if(Long_Secondary_Confirmation() == true && Long_Primary_Confirmation() == true)
   return true;
}

There are no errors in the code when compiled either

Any help or explanation for this would be greatly appreciated! :) 

 
dommm0408:

Hi, I am having issues with this function above. 

For some reason the function after the 'and' operand is not called.

The 'Long_Secondary_Confirmation' function itself does work fine, it's just when in this position in an 'if statement' it fails to call.

Similarly when i switch the two functions around to have the code in the following format, the 'Long_Primary_Confirmation' function fails to call.

There are no errors in the code when compiled either

Any help or explanation for this would be greatly appreciated! :) 

Hey.

Its impossible there are no errors unless you are compiling on MT4 and you dont have #property strict

bool Long_Confirmation()
{
   if(Long_Primary_Confirmation() == true && Long_Secondary_Confirmation() == true) return(true);
   return(false);
}  
bool Long_Primary_Confirmation(){return(true);}
bool Long_Secondary_Confirmation(){return(true);}
 
Your code
if(Long_Primary_Confirmation() == true && Long_Secondary_Confirmation() == true) return(true);
return(false);
Simplified
return Long_Primary_Confirmation() && Long_Secondary_Confirmation();
You should be able to read your code out loud and have it make sense. You would never write if( (2+2 == 4) == true) would you? if(2+2 == 4) is sufficient. So don't write if(bool == true), just use if(bool) or if(!bool). Code becomes self documenting when you use meaningful variable names, like bool isLongEnabled where as Long_Entry sounds like a trigger price or a ticket number and "if long entry" is an incomplete sentence.
 
William Roeder:
Your code
Simplified
You should be able to read your code out loud and have it make sense. You would never write if( (2+2 == 4) == true) would you? if(2+2 == 4) is sufficient. So don't write if(bool == true), just use if(bool) or if(!bool). Code becomes self documenting when you use meaningful variable names, like bool isLongEnabled where as Long_Entry sounds like a trigger price or a ticket number and "if long entry" is an incomplete sentence.

mahahahahaha

 
dommm0408:

Hi, I am having issues with this function above. 

For some reason the function after the 'and' operand is not called.

The 'Long_Secondary_Confirmation' function itself does work fine, it's just when in this position in an 'if statement' it fails to call.

Similarly when i switch the two functions around to have the code in the following format, the 'Long_Primary_Confirmation' function fails to call.

There are no errors in the code when compiled either

Any help or explanation for this would be greatly appreciated! :) 

First off, comparing your boolean functions to true and false is redundant. For the sake of readablity and style you shouldn't do it. Instead do:

if (boolFunc())
   ...
if (!boolFunc())
   ...

Next, conditional expressions stop evaluating as soon as any part of the conditional can be ultimately cause the entire conditional to be evaluated to a falsy result. That's why your second condition is never called, and that's exactly how it's supposed to work. If for some reason you need them both to be called then you need to store the result of the calls to variables first and then use them in the conditional. Putting it all together and refactoring for readability the results would be 

bool longConfirmation()
{
   bool conf1 = longPrimaryConfirmation();
   bool conf2 = longSecondaryConfirmation();
   return conf1 && conf2;
}  
 
nicholish en:

First off, comparing your boolean functions to true and false is redundant. For the sake of readablity and style you shouldn't do it. Instead do:

Next, conditional expressions stop evaluating as soon as any part of the conditional can be ultimately cause the entire conditional to be evaluated to a falsy result. That's why your second condition is never called, and that's exactly how it's supposed to work. If for some reason you need them both to be called then you need to store the result of the calls to variables first and then use them in the conditional. Putting it all together and refactoring for readability the results would be 

Thanks a lot! 

That's very insightful and a huge help!

 
dommm0408:

Hi, I am having issues with this function above. 

For some reason the function after the 'and' operand is not called.

The 'Long_Secondary_Confirmation' function itself does work fine, it's just when in this position in an 'if statement' it fails to call.

Similarly when i switch the two functions around to have the code in the following format, the 'Long_Primary_Confirmation' function fails to call.

There are no errors in the code when compiled either

Any help or explanation for this would be greatly appreciated! :) 

If the first condition is false the second is not checked. That is the reason why your second function is not called.

Run test1() and you will not get a zero divide error what proofs that after a==0 what is false the condition check is aborted.

Run test2() and you will get a zero divide error because the first condition is true and the second conditions produces the zero divide error.

 void test1(){
 
 int a = 1;
 int b = 0;
 
 if(a == 0 && 1/b == 1)
  Print("bla");

 }
 
 void test2(){
 
 int a = 0;
 int b = 0;
 
 if(a == 0 && 1/b == 1)
  Print("bla");

 }
 
Jan Tarnogrocki #:

If the first condition is false the second is not checked. That is the reason why your second function is not called.

Run test1() and you will not get a zero divide error what proofs that after a==0 what is false the condition check is aborted.

Run test2() and you will get a zero divide error because the first condition is true and the second conditions produces the zero divide error.

On a separate example, may i ask why in my code below, the first line control condition  (alertswitch is not equal to 3) is already false since global variable alertswitch has already been set to 3, yet when i run, it ignores the first statement and continue on evaluating the rest below it? (i want to evaluate this only once and not repeatedly in a loop)...even if BBConv....has been written without brackets, it should not be evaluated right? what am i missing?

if () {}...

else if (
        
         alertswitch != 3 && alertswitchPrevious != 3 
         
         && FramaCurrentPrice < botBB1  
         && BBConvDivergence == "NEUTRAL" || BBConvDivergence == "STRONG-LONG"
         && TotalScore >=2
         )
         {
         alertswitch = 3; 
         alertcomment =  "Frama-Extreme";
         Check =1; //this goes to a switch statement with Check variable '1'.
         }  

else if ( alertswitch !=5) {do this instead ....Check = 5}
 
JimSingadventure #:

On a separate example, may i ask why in my code below, the first line control condition  (alertswitch is not equal to 3) is already false since global variable alertswitch has already been set to 3, yet when i run, it ignores the first statement and continue on evaluating the rest below it? (i want to evaluate this only once and not repeatedly in a loop)...even if BBConv....has been written without brackets, it should not be evaluated right? what am i missing?

don't double post you will likely get ignored..

Reason: