Loop logic with for

Asked

Viewed 131 times

1

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);

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 in the letter To and curled up in the for (i = 0; i <= 10; i++). Why are you incrementing 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

This last example does not equal the example of the question?

  • The variable i is incremented after executing the block for, then, after the last valid execution for the block for, will still run the last increment. However, it will not fall in the clause i <= 10. Soon you’ll break the loop of for.

  • The result is 11 for the for will add up the value i+1 (in i++) and only then will you check the condition i <= 10.

  • 1

    I believe that is answered here, in particular the item m) of the response containing the step by step I can’t learn the syntax of for

2 answers

5


I imagine you understand that the loop goes up to 10, that is when you’re still at 10, it’s equal to meets the condition of being less or equal. Right?

When is the loop closed? When it is greater than 10, right? When does it occur? When the i is worth 11 the condition is false. At this point the loop is closed and nothing inside it is executed.

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

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

You have printed the current value of i, which, as we saw above, is 11 when it came out of the loop.

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

Placing some "prints* gives 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);

I put in the Github for future reference.

3

The syntax of the loop for is:

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

And the order of execution is:

  1. Initialization;
  2. Checks the condition;
  3. If the condition is true, executes the statement;
  4. Evaluates the final expression;
  5. Back to step 2;

That is, the final expression will always be evaluated at the end of each iteration. As the last one occurs when i = 10, when the final expression is evaluated, the variable will become 11.

On the computer would be something like:

  • Initialization, i = 0;
  • Checks whether i <= 10;
  • Executes the declaration;
  • Evaluates the final expression, getting i = 1;
  • Checks whether i <= 10;
  • Executes the declaration;
  • ...
  • Evaluates the final expression, getting i = 10;
  • Checks whether i <= 10;
  • Executes the declaration;
  • Evaluates the final expression, getting i = 11;
  • Checks whether i <= 10;
  • Closes the bond;

Browser other questions tagged

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