'What`s the reason for the different results in the while loops? [duplicate]

I have the following C block of code with two while loops.

#include <stdio.h>

int main()
{
    int a = 0, b = 0;

    while (a++ <= 3)
    {
        printf("cycle a = %d\n", a);
    }

    while (++b <= 3)
    {
        printf("cyclo b = %d\n", b);
    }

    return 0;
}

The output for the first while is:

ciclo a = 1
ciclo a = 2
ciclo a = 3
ciclo a = 4 

The output for the second while is:

ciclo b = 1
ciclo b = 2
ciclo b = 3

What`s the reason for the different results in the while loops?

c


Solution 1:[1]

First one is postfix increment, second one is prefix increment. You can check the while condition like this;

a++ <= 3(Firstly check a and increment a value)
Check   Output

0<=3     1
1<=3     2
2<=3     3
3<=3     4

++b <= 3(Firstly increment value and check b)

Check   Output

1<=3     1
2<=3     2
3<=3     3

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ogün Birinci