Question about MQL4 If-statements

 

If I have a statement like this: 

if(Bool1() || Bool2()){...}


where Bool1 returns True and Bool2 produces an error; does this if-statement produce an error or does Bool2() get ignored. In other words, are if-statements eagerly evaluated in the MQL4 Language?

 
TraderTogami:

If I have a statement like this: 

if(Bool1() || Bool2()){...}


where Bool1 returns True and Bool2 produces an error; does this if-statement produce an error or does Bool2() get ignored. In other words, are if-statements eagerly evaluated in the MQL4 Language?

I understand that if the first condition is true, the 2nd will not be checked as there is no need to.

Why would Bool2 produce an error?  A bool function should only return true or false.

 
Keith Watford #:

I understand that if the first condition is true, the 2nd will not be checked as there is no need to.

Why would Bool2 produce an error?  A bool function should only return true or false.

Thanks, and to answer your question, Bool2 can produce an error just like any other function. Example: 

bool Bool2(){

int x = 1/0;

int y = 2; 

return x == y;

}

 
TraderTogami #:

Thanks, and to answer your question, Bool2 can produce an error just like any other function. Example: 

bool Bool2(){

int x = 1/0;

int y = 2; 

return x == y;

}

Oh, I see. But it is not really the function producing the error, but bad coding.

 
Keith Watford #:

Oh, I see. But it is not really the function producing the error, but bad coding.

Yeah, though I have seem some people code functions where bool2 produces an error if bool1 is false (though you can argue this is bad coding as well)

 
Here an example, without bad coding:


static int array[];

int arr_size = ArraySize(array);


if( (arr_size > NULL)
 && (Array[arr_size - 1] != NULL) )
{ ... }



 
Dominik Christian Egert #:
Here an example, without bad coding:


static int array[];

int arr_size = ArraySize(array);


if( (arr_size > NULL)
 && (Array[arr_size - 1] != NULL) )
{ ... }



Very good example, I have done something similar to this in the past. 

Reason: