8
In Javascript or C, there is some difference between using for
in the normal way:
for(var i = 0; i < 10; i++){
console.log(i);
}
or execute the codes within the parentheses? Example:
for(var i = 0; i < 10;
i++,
console.log(i)
){}
8
In Javascript or C, there is some difference between using for
in the normal way:
for(var i = 0; i < 10; i++){
console.log(i);
}
or execute the codes within the parentheses? Example:
for(var i = 0; i < 10;
i++,
console.log(i)
){}
8
It is a matter of intention, this type of loop has a construction with 3 parts in its statement:
for
(has some case that makes sense to declare out), and should not declare internal variables that are not part of this control. It only happens once at the beginning before everything;Then there is the execution block that is not part of the construction of the for
in itself.
But there is an important semantic change in a specific case. I have answered in What’s the difference between while, for, while and foreach?.
In the presented form it doesn’t change anything, but if you have some condition in the middle that can make a deviation within the loop and not execute all instructions within it then it makes a difference, which is in the for
will perform in all steps that occur, what is in the block can be conditional and gives different result. It is important to make this distinction because it may seem that it is always the same thing to put there. It is the same in this case because we know that there is a conditional within the block that prevents the execution of some instruction at some step.
And if you have to break lines to do this looks really ugly and shows that it is not the appropriate way. Aesthetics counts in codes well made. People expect a for
be in a row and do a simple step control. When you invent complicates for anyone who reads your code, she has to think a little more to understand what you invented there.
So some languages don’t even like this kind of for
raw, not to give room for abuse. I like powerful and flexible languages, f0d@up who does not know how to program and abuse the resource.
I think that your image of the destroyed fiat would be right here
This case I do not think serious, even the AP seems to realize that this at least can have a difference, it is not like the people you see who will go rogue just because it works, and in the example of the question can not say that it is wrong, only ugly.
6
The for
is composed of 4 blocks, as explained by MDN documentation:
(inicialização; condição; expressão final){
declaração
}
The block expressão final
is executed after each loop iteration. With this, if you put the console.log(i)
in expressão final
, the initial value of i
will be 1
, for the i++
will only be executed after the first loop of the loop, ie will print the value of i
on the console of 1
to 10
, even though the condition is i < 10
, because the final expression will still be executed one more time when the condition is no longer met.
In the case of i
within the declaração
, the initial value shall be 0
(value declared at startup), as the i++
will only be executed after the first loop of the loop, ie the value of i
will be of 0
to 9
.
So the difference is that the code in expressão final
is executed after each loop of the loop, while in the declaração
is the opposite: before each turn.
Browser other questions tagged javascript c for encoding-style
You are not signed in. Login or sign up in order to post.
Clarity of the code.
– anonimo
@anonimo In this specific case, it makes a difference in the numbers that are printed: the first loop prints from 0 to 9 and the second loop prints from 1 to 10. See here and here
– hkotsubo
@hkotsubo: yes, but if you do:
console.log(i), i++
there will be no difference in terms of execution, which, in my opinion, is another argument to avoid using.– anonimo