Why does for loop skips the first i++

DenizAtar

In the following code,

int i;
for (i = 0; i < 5 ; i++)
System.out.println("i = " + i);

Why does the for loop skip the first i++ and output

i = 0
i = 1
i = 2
i = 3
i = 4  

Instead of

i = 1
i = 2
i = 3
i = 4

Since it is 0 at the beggining and it runs through i++ once before it executes shouldn't i start with 1?

Zabuzard

Explanation

Since it is 0 at the beggining and it runs through i++ once before it executes shouldn't i start with 1?

That is incorrect. The last part of the loop header, i.e. the C in for (A; B; C) is executed after the iteration, not before.


Execution order

So if we unroll the loop, it executes like this:

int i;
i = 0
if (i < 5) {
    System.out.println("i = " + i);
    i++;
    if (i < 5) {
        System.out.println("i = " + i);
        i++;
        ... // until the condition is false
    }
}

Hence why the first iteration has i = 0 and not i = 1.


for to while

An alternative way to express it is by using a while loop. Basically

A;
for (B; C; D) {
    E;
}
F;

is the same as

A;
B;
while (C) {
    E;
    D;
}
F;

Note how it is first E and then D, not the other way around.

So in your case we have

int i;
i = 0;
while (i < 5) {
    System.out.println("i = " + i);
    i++;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

why does it skips the first line for input of an array

Why does my fflush not work? Skips asking for string in loop

for loop skips first item in array

readline skips first line in for loop

why does lldb skips this line?

While loop skips the first `fgets()` on second iteration

Getchar() skips the first value scanned in while loop

why boost::python iterator skips first element?

I'm trying to print whole value of "fields." But it skips the first input, why?

Why does this loop break at the first iteration?

Why does the second for loop overwrite the first in Python?

Why does the loop start after I enter the first value for team A even though it's less than 15?

Why does my loop work correctly on the first iteration but not on the full set I'm looping through?

Why does my print execute after the second loop even if I use print first?

Why does remove() skips every second in array

Why does ftell skips some positions in the file?

Why does airflow skips a day for a daily dag?

why does the app skips the LoginActivity to HomeActivity?

currency converter- why does my for loop in switch wont work? when i run my code only the first for loop works

why does ++i not increment i before for loop?

Why does this while loop not execute after the first loop?

Why does my code go through the first for loop but not the second for loop?

Why is that when I use pandas to scrape a table from a website it skips the middle columns and only prints the first 2 and last 2

when i input stick the second time it doesnt print score so far like the first time, not sure why it skips this

Java while loop skips first iteration for user input

Program does nothing on first input of user (skips whole code in program)

Why does this loop never get past the first item in the list?

Why does my java guessing game not work after the first loop

Why does left justification not work in the first iteration of a loop?