Any questions from newcomers on MQL4 and MQL5, help and discussion on algorithms and codes - page 1737

 
Artyom Trishkin #:

Of course, the program is not waiting for the terminal in a separate thread to log some data.

Thanks, I mean

 
Please tell me, after break in operator for (i; i>0; i--) expression "i--" or just "expression 3" (according to documentation) is executed? I couldn't find it in the textbook.

I don't understand why "-1" is thrown from time to time if after the break the Print(i); before the break there is i-- and if (i==0), that is the exit command if i has already reached 0 inside the operator. Since i is used even further down the code, in Close[i], it produces the error "out of range

 
Ivan Butko operator for (i; i>0; i--) expression "i--" or just "expression 3" (according to documentation) is executed? I couldn't find it in the textbook.

I don't understand why "-1" is thrown from time to time if after the break the Print(i); before the break there is i-- and if (i==0), that is the exit command if i has already reached 0 inside the operator. Since i is used even further down the code, in Close[i], it produces the error "out of range

I do not understand it. Show me the code
 
Ivan Butko operator for (i; i>0; i--) expression "i--" or just "expression 3" (according to documentation) is executed? I couldn't find it in the textbook.

I don't understand why "-1" is thrown from time to time if after the break the Print(i); before the break there is i-- and if (i==0), that is the exit command if i has already reached 0 inside the operator. Since i is used even further down the code, in Close[i], it produces the error "out of range

After the break, exit the loop to the next operator after the loop, after the loop iterates to the end of the iteration at that point and starts a new one.

 
Valeriy Yastremskiy for (i; i>0; i--)
{
i--;
if (i==0) break;
}

Print(i);
Print(Close[i]);

// In the log it says (-1), how come... and, hence, the errorarray out of range, because Close[-1] doesn't happen.

I mean, am I right in understanding that after break in the header(i; i>0; i--) the third expression "i--" is still executed?

Документация по MQL5: Основы языка / Операторы / Оператор цикла for
Документация по MQL5: Основы языка / Операторы / Оператор цикла for
  • www.mql5.com
Оператор цикла for - Операторы - Основы языка - Справочник MQL5 - Справочник по языку алгоритмического/автоматического трейдинга для MetaTrader 5
 
Ivan Butko for (i; i>0; i--)
{
i--;
if (i==0) break;
}

Print(i);
Print(Close[i]);

// The log says (-1), how so... and, hence, the errorarray out of range, because Close[-1] does not happen.

I mean, am I right in understanding that after the break in the header(i; i>0; i--) the third expression "i--" is executed anyway?

Correctly writes

i=10;
for (i; i>0; i--)
{
i--;
if (i==0) break;
}

Print(i);
Print(Close[i]);
You take away not only in the header, but also in the body of the loop.
 
Konstantin Nikitin #:

It writes correctly.

Don't ask me what kind of nonsense I'm writing in my code. I just want to understand the nuances of this operator) It seems that in the body I specified "it's time to leave here, because i=0", but the operator takes and once again executes the expression, although it should pass control to the next operator

 
Ivan Butko #:

Don't ask me what kind of nonsense I'm writing. I just want to understand the nuances of this operator) It's like in the body I specified "it's time to leave here, because i=0", but the operator takes and once again executes the expression, although it should pass control to the next operator

Yes you did, but you took it away before this check. Write it this way

i=10;
for (i; i>0; i--)
{
if (i==0) break;
i--;
}

Print(i);
Print(Close[i]);

there will be a different result

 
Konstantin Nikitin #:

Yes you did, but you took it away before this check. Write it like this


aah... got it. Got it.

Thank you all for your prompt replies.

 
Ivan Butko #:

ahh... Got it. Got it.

Thank you all for your prompt replies.

Your code prints 0, not -1...

void OnStart()
  {
int i=10;
for (i; i>0; i--)
{
i--;
if (i==0) break;
}

Print(i);
  }
//+------------------------------------------------------------------+

Probably -1 printed from somewhere else, most likely the whole code is in the printers...

Before you try to solve the problem, you need to identify it... Make sure it's there and locate it before you solve it...

Reason: