17

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.

Jay
  • 9,585
  • 6
  • 49
  • 72
algo
  • 197
  • 1
  • 1
  • 5

5 Answers5

18

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.

Werner Henze
  • 16,404
  • 12
  • 44
  • 69
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
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.

David G
  • 94,763
  • 41
  • 167
  • 253
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;
}
-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.

lzam
  • 185
  • 16
-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();

}