I do not know if he's talking about some specific language. I will speak in general terms.
Some people are dogmatic.
It even makes sense for this option. Semantically% w / w% 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 interrupt then use for
. It seems to me a preciosismo that does not give any advantage. Certainly no technique. At most the idea goes that it can stop at any time and I do not have to go to the end as specified in while
. 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 she says you should follow this rule. Follow her. Not without question, of course. How are you doing. This is good. But if you do not have a problem following the established rule and "everyone" agrees with it, do not be different.
Elegance depends a little on taste. Some things are universal, some are not. For my taste I would not blindly follow it. It could be that some case I chose a construction or another one for other reasons but hardly by this presented.
Even the performance could make me choose one or the other in some specific situations.
Looking at OnoSendai's answer, something came to mind. If you make a for
that goes from start to finish. Then one fine day needs to change the implementation and find a situation that needs to have an interruption in a given situation. Would you be "forced" to change for
to for
? For what? It can do, but it does not seem necessary to me, something that will add something to the clarity of the code. On the contrary. I think in some cases using% w / w when sweeping a sequence, even if it can be stopped, may even make it less clear that this is indeed true. I think less clear, but not less, 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 the loop.
Perhaps the main advantage of while
over while
is precisely to encapsulate the control variable in the scope of the loop. If you do not need this, for
loses strength.
If you do not initialize the variable outside of the loop, the condition that the while
has has, and the step to be executed on each repetition.
This step is already criticized by some people since it is written at the beginning of the loop but only runs at the end of it. Remembering that the condition is executed in the beginning.
Of course, it's okay to declare and / or initialize the variable outside the loop, but use a for
. It only has fewer advantages.
And it's good to remember that there are languages that do not have an internal block scope in the code of a function, so that does not make any difference to them.
But the big reason for choosing one of the two constructs is just when there will be interruption of the normal flow but still inside the loop. How do I want the step to be executed if there is a jump to the end of the loop? Do you want it to always run or should the skip be omitted if there is a jump? Example:
for (i = 0; i < 10; i++) {
...
if (condiçao) {
continue;
}
...
}
The while
will be incremented even if it enters for
and the flow is bypassed. Already:
i = 0;
while(i < 10) {
...
if (condiçao) {
continue;
}
...
i++;
}
You will skip the increment of i
when you enter if
. This is a semantic change that produces a very different result. And it is not easy to simulate this using the wrong construction. Some people may try and cause more problems, even a race condition .
Has the teacher talked about this? If not, I hope you'll talk in the next class, because this is key to knowing about choosing one or the other building. This has real practical implications in the code.
Avoid interruptions
The mgibsonbr answer speaks well of this. Ideally, you should not have i
or even if
. This is break
and everyone knows that continue
is from the devil (please read my answer in link to understand sarcasm).
Well, it's a better way to goto
and although we try to avoid this type of construction, we should not do this at any cost. If you are going to write a convoluted code, it is preferable for goto
or goto
, or even break
in much rarer cases and make the code clearer.
The same goes for a continue
which is a goto
more aggressive. And using return
, even within a loop, is not a bad thing .