if (x == 0)
x = 1;
else
x = 0;
The condition checks whether x is zero.
Initial x | Condition x == 0 | Assignment | Final x |
|---|
0 | True | x = 1
| 1 |
1 | False | x = 0
| 0 |
So the operation simply flips a binary value: 0 becomes 1, and 1 becomes 0.
Option B works
x = 1 - x;
Test both allowed values:
- If
x = 0: (1-0=1) If x = 1: (1-1=0)
Both results match the original if-else.