Repeat loop logic with for [duplicate]

2

Consider the JavaScript code below.

var r = [2, 5, 6, 18, 20, 10, 23, 12, 19, 10];
var s = [1, 5, 7, 13, 18, 21, 10, 25, 32, 17, 3];
var x = [0];
var i;

for (i = 0; i <= 9; i++) {
  x[i] = r[i];
}

for (i = 0; i <= 10; i++) {
  x[i + 10] = s[i];
}

document.write(x[13] + "<br>");
document.write(i);

The values will be displayed on the screen

  • (A) 13 and 10.
  • (B) 7 and 11.
  • (C) 18 and 10.
  • (D) 13 and 11.
  • (E) 7 and 10.

Answer:

  

Answer: Letter D

I ran the code and everything OK , the answer is correct, but I was in doubt on the letter A and wrapped in for (i = 0; i <= 10; i++) . Why are you increasing once more and leaving i=11 instead of i=10 ? I ran the same function in java and if I do

for(int i = 0 ; i <= 10 ; contador = ++i) contador=11

for(int i = 0 ; i <= 10 ; contador = i++) contador=10

Does not this last example match the example of the question?

    
asked by anonymous 19.04.2018 / 23:58

2 answers

5

I imagine you understand that the loop goes to 10, that is, when it is still 10, it is the same as the condition of being less than or equal. Right?

When is the loop terminated? When it's bigger than 10, right? When does this occur? When i equals 11 the condition becomes false. At this point the loop is terminated and nothing inside it is executed.

The value does not go back alone, if the last value of i was 11, it remains 11.

Note that between a loop and another the value is 10. You may be asking why when you entered the second for and returned to 0. And it is answered by i = 0 , you have reset.

You have printed the current value of i , which, as we saw above, is 11 when you left the loop.

The difficulty is probably because it is not logically weighing, as the computer works, in steps. for is not a mechanism that counts from 0 to 10 and that's it.

Putting some "prints *" gives you a better view:

var r = [2, 5, 6, 18, 20, 10, 23, 12, 19, 10];
var s = [1, 5, 7, 13, 18, 21, 10, 25, 32, 17, 3];
var x = [0];
var i;
console.log("i = " + i);
for (i = 0; i <= 9; i++) {
  x[i] = r[i];
console.log("i = " + i);
}
console.log("i = " + i);
for (i = 0; i <= 10; i++) {
  x[i + 10] = s[i];
console.log("i = " + i);
}
console.log("i = " + i);
20.04.2018 / 00:08
2

The syntax of the for loop is:

for ([inicialização]; [condição]; [expressão final])
   declaração

And the order of execution is:

  • Initialization;
  • Check the condition;
  • If the condition is true, it executes the declaration;
  • Evaluates the final expression;
  • Return to step 2;
  • That is, the final expression will always be evaluated at the end of each iteration. As the latter occurs when i = 10 , when evaluating the final expression, the variable will become worth 11.

    On the computer it would look something like:

    • Initialization, i = 0 ;
    • Checks i <= 10 ;
    • Performs the declaration;
    • Evaluates the final expression, getting i = 1 ;
    • Checks i <= 10 ;
    • Performs the declaration;
    • ...
    • Evaluates the final expression, getting i = 10 ;
    • Checks i <= 10 ;
    • Performs the declaration;
    • Evaluates the final expression, getting i = 11 ;
    • Checks i <= 10 ;
    • Closes the loop;
    20.04.2018 / 00:09