Questions from Beginners MQL5 MT5 MetaTrader 5 - page 495

 

On the first case is solved - put in brackets for two comparisons and it does not swear - was there a logical error - i.e. the results of the comparison could be replaced because of the "or"?

There remains an open question with the second expression - what is wrong there - any ideas?

 
-Aleks-:

The first case is solved - put in brackets two comparisons and it does not swear - was there a logical error - i.e. comparison results could be replaced because of "or"?

There is still an open question concerning the second expression - what is wrong there - any ideas?

Remember that the check of a condition in the new mql4 is not performed completely. I.e. in the condition

if(Low[X]>PriceBuy && High[X]>PriceBuy ||
               Low[X]<PriceBuy && High[X]<PriceBuy)

If Low[x] is not larger than PriceBuy, then no further check will be performed. This is what the error description tells us about. Correspondingly, if the first pair is enclosed in brackets, the second pair of conditions will be checked if it fails.

Second question.

for(calc_day;calc_day>0; calc_day--)

What is calc_day equal to? May it be less than zero? The compiler doesn't know what's in the logic... That's why it warns about a possible ineffective loop.

 
Alexey Viktorov:

Remember that condition checking in the new mql4 is not performed completely. I.e., in the condition

if(Low[X]>PriceBuy && High[X]>PriceBuy ||
               Low[X]<PriceBuy && High[X]<PriceBuy)

If Low[x] is not greater than PriceBuy, no further check will be performed. This is what the error description tells us about. Correspondingly, if the first pair is enclosed in brackets, the second pair of conditions will be checked if it fails.

Thank you for the answer!

Surprised - as visually the logic has always worked correctly, hmmm...


Alexey Viktorov:

Second question.

for(calc_day;calc_day>0; calc_day--)

What is calc_day equal to? May it be less than zero? The compiler doesn't know what is in the logic... That's why it warns about a possible ineffective loop.

The calc_day number is always greater than 0. Other values don't call the code.
 
-Aleks-:

Thanks for the reply!

Surprised - as visually the logic has always worked correctly, hmmm...

Before 6** builds it was like that, checked the whole chain of conditions and everything worked, but now everything has changed. And in old builds in this case condition is ambiguous. How is it designed?

if( (Low[X]>PriceBuy && High[X]>PriceBuy) || (Low[X]<PriceBuy && High[X]<PriceBuy) )

Or did it?

if(Low[X]>PriceBuy && ( High[X]>PriceBuy) || (Low[X]<PriceBuy ) && High[X]<PriceBuy)

A human brain can figure it out, but what is a compiler to do? It doesn't have a brain.

-Aleks-:

The number calc_day is always greater than 0. Other values don't cause code to be called.

Tell it to the compiler...

 
Alexey Viktorov:

Up to build 6** it was like that, the whole chain of conditions was checked and everything worked, but now everything has changed. And in old builds the condition is ambiguous in this case, too. How is it designed?

if( (Low[X]>PriceBuy && High[X]>PriceBuy) || (Low[X]<PriceBuy && High[X]<PriceBuy) )

or is that it?

if(Low[X]>PriceBuy && ( High[X]>PriceBuy) || (Low[X]<PriceBuy ) && High[X]<PriceBuy)

A human brain can figure it out, but what is a compiler to do? He doesn't have a brain...


Tell it to the compiler...

Convincing - I'll be more vigilant.

How do I tell the compiler? Write it, but will it understand?

if (calc_day<0)  calc_day=calc_day*(-1);

 
Alexey Viktorov:

It was like that before 6** builds, the whole chain of conditions was checked and everything worked, but now everything has changed. Yes and in old builds in this case condition ambiguity. How is it designed?

if( (Low[X]>PriceBuy && High[X]>PriceBuy) || (Low[X]<PriceBuy && High[X]<PriceBuy) )

or is it?

if(Low[X]>PriceBuy && ( High[X]>PriceBuy) || (Low[X]<PriceBuy ) && High[X]<PriceBuy)

A human brain can figure it out, but what is a compiler to do? He doesn't have a brain...


Tell it to the compiler...

check operator precedence for possible error; use parentheses to clarify precedence - this is not an error. It is a warning. It occurs in old codes.

They added it for the user rather than for the compiler. The compiler is guided by operation priorities. Everything is clear there. Priority of && is higher than that of ||. That's why the brackets should be arranged in this way

if( (Low[X]>PriceBuy && High[X]>PriceBuy) || (Low[X]<PriceBuy && High[X]<PriceBuy) )

Update: In this case both for MT4 and MT5 the priority is the same.

 
-Aleks-:

Convincing - I'll be more vigilant.

How do I tell the compiler? Write it, but will it understand?

if (calc_day<0)  calc_day=calc_day*(-1);

for( calc_day = 365; calc_day > 0; calc_day --)
So try it.
 
Alexey Kozitsyn:

check operator precedence for possible error; use parentheses to clarify precedence - this is not an error. It is a warning. It occurs in old codes.

We added it for the user rather than for the compiler. The compiler is guided by operation priorities. Everything is clear there. Priority of && is higher than that of ||. That's why the brackets should be arranged in this way

if( (Low[X]>PriceBuy && High[X]>PriceBuy) || (Low[X]<PriceBuy && High[X]<PriceBuy) )

Update: In this case both for MT4 and MT5 the priority is the same.

Well in essence, yes, the warning is not an error. But how will the initial variant work if( Low[X]>PriceBuy && High[X]>PriceBuy || Low[X]<PriceBuy && High[X]<PriceBuy )? Or is it a bummer to put a couple of brackets?

 
-Aleks-:

Convincing - I'll be more vigilant.

How do I tell the compiler? Write it, but will it understand?

if (calc_day<0)  calc_day=calc_day*(-1);

for(int i=calc_day; i>0; i--) {}
 
Alexey Viktorov:

Well, in essence, yes, the warning is not an error. But how will the initial variant work if( Low[X]>PriceBuy && High[X]>PriceBuy || Low[X]<PriceBuy && High[X]<PriceBuy )? Or will it fail to put a couple of brackets?

It will work the way I wrote above.

The compiler warns the user not to be lazy and put parentheses to avoid possible non-obvious errors.

Reason: