I don’t know if he’s speaking any specific language. I’m going to speak in general terms.
Some people are dogmatic.
This option even makes sense. Semantically the for would indicate that you want to go from one point to another and there should be no interruptions. Some people will say that if there is to be an interruption then use a while. It seems to me a preciousness that gives no advantage, certainly no technique. At most it passes the idea that it can stop at any time and does not need to go to the end as specified in for. This is if everyone involved with the code follows this rule.
There is no mistake in this, but if you work on a team (your class is your team right now) and it says you should follow this rule, follow it. Not without questioning, of course, how you’re doing. That’s good. But if you don’t have a problem following the established rule and "everyone" agrees with it, don’t be different.
Elegance depends a little on taste. Some things are universal, others do not. For my taste I would not follow this blindly. Could be that some case I chose a construction or other for other reasons but hardly for this presented.
Even the performance could make me choose one or the other, in some specific situations.
Looking at Onosendai’s reply something came to my mind: you make a for that goes from start to finish, then a beautiful day needs to change the implementation and finds a situation that needs to have an interruption in a given situation. You would be "obliged" to change the for for while? Why? You can do it, but it doesn’t seem necessary, something that will add something to the clarity of the code, on the contrary. I think in some cases use a while when it sweeps a sequence, even if it can be stopped, it may even make it less clear what that is in fact. I think less clear, but not much less, I would also not strongly oppose this change, the code would still be clear enough.
Reasons to choose one of the two
A good reason to opt for while is when the control variable will be used outside of the loop.
Perhaps the main advantage of for about the while is precisely encapsulate the control variable in the loop scope. If you don’t need this, the for loses strength.
If you do not initialize the variable outside of the loop the condition remains, that the while has, and the step to be executed in each repeat.
This step is already criticized by some people since it is written at the beginning of the loop but is only executed at the end of it. Remembering that the condition is executed at the beginning.
Of course it’s okay to declare and/or initialize the variable outside of the loop using a for. There are only fewer advantages.
And it’s good to remember that there are languages that have no internal block scope in the code of a function, so it makes no difference to them.
But the big reason to choose one of the two constructions is precisely when there will be interruption of the normal flow but still within the loop. How do you want the step to be executed if there is a jump to the end of the loop? Do you want it to run every time or should the step be omitted if there is a jump? Example:
for (i = 0; i < 10; i++) {
...
if (condiçao) {
continue;
}
...
}
The i will be increased even if it enters the if and the flow is diverted. Already:
i = 0;
while(i < 10) {
...
if (condiçao) {
continue;
}
...
i++;
}
I put in the Github for future reference.
It will skip the increment of i when you enter the if. This is a semantic change that produces a very different result. And it’s not easy to simulate this using the wrong construction. Some people may try and cause bigger problems, even a running condition.
Did the professor talk about this? If not, I expect you to speak in the next class, because this is something fundamental to know about the choice of one or the other construction. This has real practical implications in the code.
Avoid interruptions
The mgibsonbr response speaks well about this. The ideal is not to have break or even a continue. This is a goto and everyone knows that goto is of the devil (please read my reply on link to understand the sarcasm).
Well, it’s a better way to goto And although we try to avoid this kind of construction, we should not do this at any cost. If it’s to write a convoluted code, it’s preferable to break or continue, or even a goto in much rarer cases and make the code clearer.
The same goes for a return which is a break more aggressive. And wear a return, even inside a loop, it’s not something bad.
I wonder when he said that, he was thinking about
forthat we use by setting a number so much that we want to iterate. Because in that case, if we know the number, there would be no need to use thebreak. We must remember that theforcan be used for many other operations in addition to increments of1..9.– Wallace Maxters
He was giving examples with all operators and stating that it should be and only used for cases we know how far to go. In other cases, we should opt for while or while.. That’s why I was confused about this, because I use it a lot to iterate list and look for something I want and stop the process
– Paulo Gustavo
@Wallacemaxters even in this case may have several reasons to break the
for. It is common to have code that must go from 0 to 9 unless he discovers something along the way. It will only be ridiculous if he makes aforthis way and have a break like this:if (i == 5) break;– Maniero
@Wallacemaxters I guess you didn’t understand what I wrote, but okay.
– Maniero