# Thread: How this code works?

1. ## How this code works?

int i=0, j=1;
j %= (i++ || i++==j);
printf("El valor de i es %d\n", i);
printf("El valor de j es %d\n", j);

what's the meaning of ´j%´?

and what happend with this (i++ || i++==j) 2. %= is known as an assignment operator, where

j += 1

is shorthand for
j = j + 1

> and what happend with this (i++ || i++==j)
It's a boolean expression which will have the value 0 (if false), or 1 (if true).

Knowledge of short-circuit evaluation rules will be important. 3. Here is the result I got:
Code:
```El valor de i es 2
El valor de j es 0```
Which makes sense if you consider:
Order of evaluation - cppreference.com
6) Every value computation and side effect of the first (left) argument of the built-in logical AND operator && and the built-in logical OR operator || is sequenced before every value computation and side effect of the second (right) argument. 4. With the two instances of i++ in the same line, is this not also an example of undefined behavior? 5. Originally Posted by Elkvis With the two instances of i++ in the same line, is this not also an example of undefined behavior?
Evaluation is specified for ++i and i++ and before or after evaluation. Evaluation in this case means specifically the value of the variable, not the outcome of the function, so it shodul always have the effect of e.g.

x = 5 then

5 || 6

with x being 7 after the fact.

I wouldn't write such a piece of code though, as this may be an area where some compilers are non-compliant (ahem MSVC). 6. Originally Posted by Elkvis
With the two instances of i++ in the same line, is this not also an example of undefined behavior?
No, because || introduces a sequence point. Originally Posted by abachler
Evaluation is specified for ++i and i++ and before or after evaluation. Evaluation in this case means specifically the value of the variable, not the outcome of the function, so it shodul always have the effect of e.g.

x = 5 then

5 || 6

with x being 7 after the fact.
Not quite: the issue is that between consecutive sequence points, if i is modified more than once, then the behaviour is undefined. Thankfully, || introduces a sequence point, so there is no issue. If it were | instead, then the behaviour would be undefined. 7. abachler! Haven't seen you on here in over 4 years! Where have you been? What have you been up to? Popular pages Recent additions #### Tags for this Thread

%dn, i++, meaning, printfel, valor 