What’s the difference between executing code inside or outside the "for" keys?

Asked

Viewed 118 times

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)
){}
  • 4

    Clarity of the code.

  • @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

  • 1

    @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.

2 answers

8


It is a matter of intention, this type of loop has a construction with 3 parts in its statement:

  • a state-of-control initialization of the steps of each execution, and all variables involved in it should be declared there, not outside, not inside, should be in the 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;
  • an output condition that is executed and verified at the end of each step, and only continues the loop if the condition is true, one can abuse but should only make the basic condition, not invent things (there is always something case that can be useful to abuse, but you have to be very aware of what you are doing and be very necessary).
  • a mandatory execution instruction at the end of all steps, shall not place anything there that does not change the conditions of execution of the loop. It is abuse to put other things even if it works and get the expected result.

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

  • 1

    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

You are not signed in. Login or sign up in order to post.