Execution or not of the increment in loops for

Asked

Viewed 97 times

1

Code 1:

var x = 1; 
  
for( ; x<6 ; x+=2 ){ 
    x=x*x; 
} 

console.log(x);

In the above code even if the condition is false the incrementing part is last executed.

Code 2:

 var x = 0; 

for( ; x<8 ; x++ ){} 
console.log(x); 

for( ; x>4 ; x-=2 ){} 
console.log(x); 

In the above code even if the condition is false the incrementing part is not executed.

Why does this happen or am I making a mistake?

  • 1

    Can do table test? If yes, try to do in both codes. I particularly did not understand the questions.

  • @Lucas Costa, it is simple, in the first code even x being greater than 6 the part of the increment of the for(;(this part here)) it is executed, that is, there is the increment of x and it becomes 11 at the end. In the second code when x < 8 the increment part of x is not executed, in the same way with the second is, that is, when x is 8 the part x++ is not executed, so much so that the value of x is 8 at the end of the code. The question is: why is logic different for both?

  • 1

    The debugger is your friend.

3 answers

5

Guy the For works the same in both cases, the problem is that your comparison was glitch because in a place you started with 0 and the other with 1.

With the exception of the For increments before comparing. For

So even 9 being greater than 6 it will still increase 2 to then compare.

See the example below displaying step by step:

var x = 1; 
for( ; x<6 ; x+=2 ){ 
    x=x*x; 
    console.log('x:' + x);
} 
console.log('Final x:' + x);

var y = 1; 
for( ; y<8 ; y++ ){
  console.log('y: ' + y); 
} 
console.log('Final y: ' + y); 

4


In the first part he enters the for with x = 1 and makes x = x * x and 1, then enters with x = 3 and makes x = x * x that of 9, this occurs inside the loop, now he leaves the loop and makes x+=2 then x = 11, back to see if x < 6 of false and printa 11;

Same logic in the second.

is more or less like this:

x = 1 x < 6? true x = x * x x = 1 x += 2 x = 3 x < 6? true x = x * x x = 9 x += 2 x = 11 x < 6? false

  • That’s where the 'Q' of the question is, @Hiogo, the result I expected was 9, but that’s 11. Rotate there to see ;)

  • 1

    Now I understand your doubt, the x=x*x is inside the is, when the loop is over it executes x+=2 when x = 9

  • but the logic of whatever you’ve developed is flawed, it would be like,not: x=3; x<6=True; x=x*x; x=9; x< 6=False. Or am I wrong? Why does it increase with 2 more without first checking the condition? (according to the logic shown above)

  • it is entering the loop for 2 time, when finished it does x+=2, I edited the answer to be clearer

  • Your doubt is not really about javascript, you are confused on some programming logic concepts, mainly scope.

  • 1

    I was thinking of the for sequence in the wrong way, actually it’s like this: variable is initialized / condition check / execution of what is in the for body / then variable increment. Solved my doubt. Thanks to all!

Show 1 more comment

1

Although the instruction on increment comes at the beginning of the loop for, you can consider that this increment actually runs just after the body of the loop, before the next check on the stop condition. So:

for(var i=0; i<10; i++) {
   // corpo do loop

   // incremento executado aqui
   // sai do loop com i === 10
}

Now, if the stop condition is false from the start, neither the loop body nor the increment will be executed:

var i = 10;
for( ; i<10; i++) {
    // nunca executa nem o corpo nem o incremento
}
// aqui, ainda temos i === 10

Browser other questions tagged

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