Post increment in while loop in C

Here is a very simple C program:

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

Result:

 1 2 3 4 5 6 7 8 9 10 

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

 1 2 3 4 5 6 7 8 9 

For both i++ and ++i first number is 1 .
I expected to see 0 for incremental increments during the while() . Thanks.

+5
source share
6 answers

i++ (and ++i ) is performed as part of the evaluation of the while expression that occurs before printing. Thus, this means that it will always print 1 initially.

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

 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 

Say i gets up to 9 . Using i++ < 10 it uses 9 < 10 for the while expression, and then i prints to 10 before printing. Thus, the check uses 9, then prints 10.

C ++i < 10 it first increments i , and then uses 10 < 10 to express while . Thus, the check uses 10 and does not print anything, because it exited the loop from this check.

+5
source

i++ is a post-increment, and ++i is a pre-increment. Post-increment means that the previous value is returned after the object is incremented. Pre-incrementation means that the object grows and then returns. In any case, the object grows when its expression is evaluated, and why you do not get 0 as the first output.

+1
source

You increment i after checking and before printing it. Either increment after checking and printing, or using the do while :

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

This is because you already increased i before you called the print command. A.

So, if you want to start with print 0 ,

you may try:

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

So, you increase it after printing; this is post-increment .

If you change it to ++i as follows, you will get 1 from the first print. A.
This is a pre-increment .

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

I think now you will have an understanding of post-increment and pre-increment .

0
source

When the while while while(i++ < 10) is evaluated, it checks the value of i, adds it to it and compares the old value with 10. When you change it to while(++i < 10) , it increases the value of i to comparing the new value with 10.

In any case, by the time you get to the next operator i , it has already been increased.

If you want to print from 0 to 9, you can try

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

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

0
source

Whie loops verify that conditions are met during the block and check

the condition falsely terminates the while loop.

see about increment by mail over a cycle ++

void main ()

{

int rmvivek;

clrscr ();

printf ("an integer from 1 to 10");

ssapE ("% d", & rmvivek);

while (10> = rmvivek ++)

{

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

}

Getch ();

}

-1
source

Source: https://habr.com/ru/post/1201285/


All Articles