++ in front of variable inside of a while - Language C

0
int bandera;
while (++bandera != 1) {
    bandera--;
}

I do not understand the code of ++bandera != 1 . Does it mean that +1 is added to the flag and then fixed if it is different from 1?

    
asked by franncapurro 25.09.2017 в 06:05
source

1 answer

2

Exactly. The pre-increment operator, which is what it is called, first modifies the variable and then leaves it available for the expression in which it is used. The post-increment on the contrary first uses the value and then increases the variable. In both cases, the value of the variable is increased. Something similar happens with the pre- and post-decrement ones.

One way to see it working would be:

int a = 0, b = 0;
printf(“pre: %d, post: %d\n”, ++a, b++);
printf(“a: %d, b: %d\n”, a, b);

The result is:

  

pre: 1, post: 0

     

a: 1, b: 1

    
answered by 25.09.2017 в 06:17