Where Would Control Be Passed To Next In This 'While' Loop?

130

Hi everyone.

I am reading through the MQL4 manual step by step just trying to make sure I understand each section as much as possible before moving on.

I have some code here taken directly from page (https://book.mql4.com/operators/break) of the manual:

```int start()                             // Special function start()
{
//--------------------------------------------------------------------------
int
L,                                  // Thread length
S_etalon=1500000,                   // Predefined area (m²)
S,                                  // Area of the rectangle
a,b,s;                              // Current side lengths and area
//--------------------------------------------------------------------------
while(true)                         // External cycle for thread lengths
{                                 // Start of the external cycle
L=L+1000;                        // Current thread length of в mm
//--------------------------------------------------------------------
S=0;                             // Initial value..
// ..for each dimension
for(a=1; a<L/2; a++)             // Cycle operator header
{                              // НStart of the internal cycle
b=(L/2) - a;                  // Current side lengths
s=a * b;                      // Current area
if (s<=S)                     // Choose the larger value
break;                     // Exit internal cycle
S=s;                          // Store the best value
}                              // End of the internal cycle
//--------------------------------------------------------------------
if (S>=S_etalon)                 // Choose the larger value
{
Alert("The thread length must be ",L/1000," m.");// Message
break;                         // Exit the external cycle
}
}                                 // End of the external cycle
//--------------------------------------------------------------------------
return;                              // Exit function operator
}
//--------------------------------------------------------------------------```

My question is regarding the line:

`if (S>=S_etalon)`

Of course if this condition returns as 'true', then control can be passed to the first line in the body of the 'if' operator.

But if the condition were to return as 'false', then control should be passed to the nearest following operator.  As there is no 'nearest following operator' below the condition of the 'if' statement, should control therefore go straight back to the header of the 'while' loop because all the the possible calculations of this iteration have been completed, or somewhere else?

Help and advice much appreciated.

Thanks,
Ryan.

Operator 'break' - Operators - MQL4 Tutorial
• book.mql4.com
In some cases, for example, when programming some cycle operations, it could become necessary to break the execution of a cycle before its condition becomes false. In order to solve such problems, you should use the operator 'break'. Format of the Operator 'break' The operator 'break' consists of only one word and ends in character...
18240

koranged: As there is no 'nearest following operator'

```      if (S>=S_etalon)                 // Choose the larger value
{
Alert("The thread length must be ",L/1000," m.");// Message
break;                         // Exit the external cycle
}
}                                 // End of the external cycle```

Of course there is. It's the end of the while.

130

whroeder1:

Of course there is. It's the end of the while.

You have cut out part of my sentence. My statement "As there is no 'nearest following operator' below the condition of the 'if' statement" is relevant as there is NO nearest following operator BELOW the condition of the 'if' statement.

So if as you say 'it's the end of the while.' then that is not BELOW the condition of the 'if' statement but rather ABOVE the condition of the 'if' statement.

I am effectively asking if control gets passed back up to the operator header, being that there is no following operator BELOW the line 'S>=S_etalon' (assuming that the condition is false.)

Can you help answer my question or do you wanna have a second go at being a smart arse?

18240

1. koranged: So if as you say 'it's the end of the while.' then that is not BELOW the condition of the 'if' statement but rather ABOVE the condition of the 'if' statement.
```      if (S>=S_etalon)                 // Choose the larger value       <v Start of the IF
{
Alert("The thread length must be ",L/1000," m.");// Message
break;                         // Exit the external cycle
}                                                               <^ End of the IF
vv Below the if. This is the next following.
}                                 // End of the external cycle     << End of the WHILE is below end of the IF.```
The next operation is the end of the while.

2. koranged: Can you help answer my question or do you wanna have a second go at being a smart arse?
Do you really expect people to continue trying to help you with your rude remark? This question (and the other,) are so basic. You really have to learn how to code.
Moderator
37217

koranged:

You have cut out part of my sentence. My statement "As there is no 'nearest following operator' below the condition of the 'if' statement" is relevant as there is NO nearest following operator BELOW the condition of the 'if' statement.

So if as you say 'it's the end of the while.' then that is not BELOW the condition of the 'if' statement but rather ABOVE the condition of the 'if' statement.

I am effectively asking if control gets passed back up to the operator header, being that there is no following operator BELOW the line 'S>=S_etalon' (assuming that the condition is false.)

Can you help answer my question or do you wanna have a second go at being a smart arse?

130

whroeder1:
1. The next operation is the end of the while.

2. Do you really expect people to continue trying to help you with your rude remark? This question (and the other,) are so basic. You really have to learn how to code.

My questions are basic because I am an absolute beginner who is literally reading step by step through the manual to gain this information. I like to check the information I have learnt with the MQL4 community because there are people on here who know far more than me and can point me in the right direction if I have misinterpreted something.

Would it be possible for you to actually answer my initial question:

Where is control passed to next?

Are you saying it is passed back into the header of the 'while' operator as the first iteration of the cycle has been completed?

If so then that was what I asked in my very first message:

'But if the condition were to return as 'false', then control should be passed to the nearest following operator.  As there is no 'nearest following operator' below the condition of the 'if' statement, should control therefore go straight back to the header of the 'while' loop because all the the possible calculations of this iteration have been completed, or somewhere else?'

Your response 'Of course there is. It's the end of the while.', does not answer the question 'where is control passed to next'. You simply picked out half a sentence and made a comment on it, without answering the actual question, which is why I called you 'smart arse'.

However I will offer you my apologies as regardless of the reason, calling you that was uncalled for so I am sorry.

936

Curly brackets are an operator, kind of. If I understand correctly control is passed to the last curly bracket which ends the loop and then to the while for another round..
130

kypa:
Curly brackets are an operator, kind of. If I understand correctly control is passed to the last curly bracket which ends the loop and then to the while for another round..

Yeah that's how I see it too. I wasn't aware curly brackets are classed as an operator to be honest, but I suppose they are a formality. So once the closing curly bracket of the external cycle comes in I will assume that is the current iteration complete, and that control is sent back into the 'while' header for the next iteration.

936

It's in Documentation.

https://www.mql5.com/en/docs/basis/operators

Documentation on MQL5: Language Basics / Operators
• www.mql5.com
Language operators describe some algorithmic operations that must be executed to accomplish a task. The program body is a sequence of such operators. Operators following one by one are separated by semicolons. Performs an operator until the expression checked becomes false. The end condition is checked, after each loop. The loop...
18240

koranged:

Would it be possible for you to actually answer my initial question:

Where is control passed to next? Are you saying it is passed back into the header of the 'while' operator as the first iteration of the cycle has been completed?

1. I did. "If the condition were to return as 'false', then control should be passed to the nearest following operator." Control goes to the bottom of the while loop. What happens next was not your initial question.
2. Whether the while loop goes back to the top or exits, depends on the variable test of the while. Not your initial question.
130

whroeder1:
1. I did. "If the condition were to return as 'false', then control should be passed to the nearest following operator." Control goes to the bottom of the while loop. What happens next was not your initial question.
2. Whether the while loop goes back to the top or exits, depends on the variable test of the while. Not your initial question.

1. Okay I didn't realise that a closing brace was classed as an operator so that is why I was misunderstanding you.

My question was 'where is control passed to next' and you were effectively saying control is passed to the closing brace of the 'while' operator, and then back into the 'while' header for next iteration (correct me if I'm wrong).

If that is the case then I offer you a further apology for my misunderstanding.

2. I did actually specify in the question - 'if the condition were to return as false'.

If the condition returns true then control obviously goes to the body of the 'if' operator.

So I was only asking where control would go to if the condition tested false.