betaD
betaD

Reputation: 55

for loops in java and values of i

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)

Upvotes: 2

Views: 5196

Answers (8)

geek_10101010
geek_10101010

Reputation: 90

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.

Upvotes: 0

Roger Lindsj&#246;
Roger Lindsj&#246;

Reputation: 11553

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

Upvotes: 7

Mayank K Rastogi
Mayank K Rastogi

Reputation: 604

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.

Upvotes: 0

H&#252;lya
H&#252;lya

Reputation: 3433

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

Upvotes: 0

Fato
Fato

Reputation: 223

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?

Upvotes: 0

SamHoque
SamHoque

Reputation: 3154

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.

Upvotes: 0

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522762

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.

Upvotes: 3

Saurav Sahu
Saurav Sahu

Reputation: 13994

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

Upvotes: 1

Related Questions