2

In Horstmann's textbook(Big Java Late Objects) on java, for a for loop such as :

for (i = 0; i <= 5; i++)

Horstmann says that values of i for this for loop is 0 1 2 3 4 5 .

However, it seems to me that the value of i should end at 6 since the loop is entered when the i has the value 5 . What am I not understanding about this loops could someone explain me?

(PS. I am sorry if the question is too basic and thus not allowed in this platform.)

edit about the duplicate suggestion: My question is not a duplicate of the suggested link because the suggested link is about the execution of the for loop, mine is about the updating of the variable i , after the end of the execution. I understand that if I would add a System.out.print(i) statement the output will be 0 1 2 3 4 5 because i gets updated after the execution statement (in this case the print statement)

betaD
  • 55
  • 1
  • 5
  • Possible duplicate of [How does a for loop check its conditions in Java?](https://stackoverflow.com/questions/20450056/how-does-a-for-loop-check-its-conditions-in-java) – Johan Oct 12 '18 at 08:49
  • 1
    Yes, but the accepted answer in the duplicate also states that the update of `i` is executed after the execution of the body (before the termination condition). The author of that question originally asked why the loop printed `i=5` when the condition was set to `i<5` (which was because the author had combined the print statement with the increment part. – Johan Oct 12 '18 at 09:14
  • 1
    That is a compelling answer, and I am willing to delete this question, due to it being a duplicate. However I also think that I should not because @Roger Lindsjö's answer is more illuminating the ones that were given as an answer to the suggested post. In that case it appears to me as though there is a tie, and I am willing to act in accordance to a tie breaking suggestion . – betaD Oct 12 '18 at 09:43
  • 1
    I think you should consider this the difference between what is observable (highest value of `i` seen inside the loop body is 5) and what happens 'under the hood' (`i` becomes 6 so the loop terminates) – Mark Rotteveel Oct 12 '18 at 15:32

8 Answers8

6

You are correct that the value of i will be 6 after the loop has terminated, but perhaps Horstmann meant the values of i inside the loop?

int i;
for (i = 0; i <= 5; i++) {
    System.out.println("Value of i IN loop: " + i);
}
System.out.println("Value of i AFTER loop: " + i);

Output:

Value of i IN loop: 0
Value of i IN loop: 1
Value of i IN loop: 2
Value of i IN loop: 3
Value of i IN loop: 4
Value of i IN loop: 5
Value of i AFTER loop: 6

And it is customary to declare the "counter" for the loop in the loop unless the value when terminating is needed afterwards.

for (int i = 0; i <= 5; i++) {
}
// i is not available here
Roger Lindsjö
  • 11,330
  • 1
  • 42
  • 53
  • This makes a lot of sense, afterall it reminded me that the i variable only exists inside the foor loop and "dies" when the loop is no longer executed. – betaD Oct 12 '18 at 09:11
3

Here is the anatomy of a for loop in Java (similar applies to C/C++ and a few other languages as well)

for (int i=0; i <= 5; ++i)

int i=0     initial condition; happens before the loop starts
i <= 5      check is performed BEFORE each iteration of the loop
++i         loop variable is incremented AFTER each iteration

So, your for loop would iterate 5 times, and at the end of the fifth iteration, i would be incremented to 6. At that point, the i <= 5 check would happen one last time, and it would fail, since 6 is greater than 5.

To convince yourself of all this, run the following Java code:

int i;
for (i=0; i <= 5; ++i) {
    // empty
}
System.out.println(i);

You will see that the value of i after the loop in fact is 6.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
1

This loop

for ( i = 0; i <=5; i ++)

is like

int i = 0;

while (i <= 5){  // Exits when i > 5

   ...

   i++;
} // goes back to while-loop check
Saurav Sahu
  • 13,038
  • 6
  • 64
  • 79
0

The value of I will end at 5, Because you are doing i <= 5 It checks if I is greater or equal to 5 and then ends the loop. hence making it end at 5 and not 6.

SamHoque
  • 2,978
  • 2
  • 13
  • 43
0

Well the man is right! it iterates from 0 to 5, because... 1. the condition is „i <= 5“ 2. Java is 0 indexed, or to be exact by declaring and initialising the variable i = 0, it will start iterating from 0 until the condition returns false. The decisive point here is the conditional pressure inside the for-loop You understand?

Fato
  • 203
  • 2
  • 10
0

This is the working sequence with values to see what exactly happens:

i=0 

//tasks

i++ 

i=1 check i<=5 //ok

//tasks

i++

i=2 check i<=5 //ok 
//tasks

i++

i=3 check i<=5 //ok 
//tasks

i++

i=4 check i<=5 //ok 
//tasks

i++

i=5 check i<=5 //ok 
//tasks

i++

i=6 check i<=5 //it's not ok

// exit the loop
Hülya
  • 3,353
  • 2
  • 12
  • 19
0

You are correct that the value of i gets incremented to 6, and then the loop condition does not get satisfied,and the loop exits. However, the author is trying to convey that the values of i for which the for loop block will get executed, are 0, 1, 2, 3, 4, 5.

0

The for loop will end in 5 since the condition on your loop is i<=5, meaning the loop will stop if it reaches the number 5.

geek_10101010
  • 90
  • 1
  • 4
  • 19