Post Increment in while loop in C
Asked Answered
K

5

18

Here is a very simple C program:

int main()
{
    int i = 0;
    while(i++ < 10)
         printf("%d\n", i);

    return 0;
}

The result is:

1
2
3
4
5
6
7
8
9
10

Why 0 is not the first number to print? And if I replace the i++ with ++i I'll get this:

1
2
3
4
5
6
7
8
9

For both i++ and ++i the first number is 1.
I expected to see the 0 for post increment in while loop while().
Thanks.

Knockknee answered 27/8, 2014 at 23:49 Comment(4)
i++ is incrementing in execution time, of course it's one.Jardena
Regardless of operator used, you print i after it was modified, so how can you see 0!?Evolutionary
while(i++ < 10) /*i is incremented here*/ printf("%d\n", i);Weighted
C programmers love to downvote on SO for some reason, it's like C is not the most confusing language ever existed.Somewhere
A
19

The i++ (and ++i) is done as part of the while expression evaluation, which happens before the printing. So that means it will always print 1 initially.

The only difference between the i++ and ++i variants is when the increment happens inside the expression itself, and this affects the final value printed. The equivalent pseudo-code for each is:

while(i++ < 10)            while i < 10:
                               i = i + 1
    printf("%d\n", i);         print i
                           i = i + 1

and:

                           i = i + 1
while(++i < 10)            while i < 10:
    printf("%d\n", i);         print i
                               i = i + 1

Let's say i gets up to 9. With i++ < 10, it uses 9 < 10 for the while expression then increments i to 10 before printing. So the check uses 9 then prints 10.

With ++i < 10, it first increments i then uses 10 < 10 for the while expression. So the check uses 10 and doesn't print anything, because the loop has exited because of that check.

Aranda answered 27/8, 2014 at 23:52 Comment(0)
S
6

i++ is post-increment and ++i is pre-increment. Post-increment means that the previous value is returned after incrementing the object. Pre-increment means that the object is incremented and then returned. Either way, the object is incremented when its expression is evaluated and that's why you don't get 0 as the first output.

Strenuous answered 27/8, 2014 at 23:55 Comment(0)
G
0

You increment i after checking it and before printing it. Either increment after checking and printing, or use a do while loop:

int main()
{
    int i = 0;
    do {
        printf("%d\n", i);
    } while(i++ < 10);
    return 0;
}
Guay answered 27/8, 2014 at 23:59 Comment(0)
A
-1

When the while loop while(i++ < 10) is evaluated it is checking the value of i, adding one to it, and comparing the old value to 10. When you change it to while(++i < 10) it increments the value of i before comparing the new value to 10.

Either way however, by the time you get to the next statement i has already been incremented.

If you wanted to print from 0 to 9 you could try

    while(i < 10)
     printf("%d\n", i++);

instead. In this case the value of i is incremented after providing its original value to the printf statement.

Arithmetician answered 28/8, 2014 at 0:14 Comment(0)
E
-3

The whie loops is check the conditions is true executed while block and test the

conditions is false terminate the while loop.

see about post increment within while loop ++

void main()

{

int rmvivek;

clrscr();

printf("ënter the integer values 1 to 10");

scanf("%d",&rmvivek);

while(10>= rmvivek++)

{

printf("the value is=%d\n", rmvivek);

}

getch();

}

Executor answered 14/10, 2014 at 10:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.