Multiple choice

x = 3, counter = 0; while ((x-1)) { ++counter; x--; } Referring to the sample code above, what will be the value of variable counter after the execution of the code?

  1. 0

  2. 1

  3. 2

  4. 3

  5. 4

Reveal answer Fill a bubble to check yourself
C Correct answer
Explanation

The loop continues while (x-1) is non-zero. x starts at 3: (3-1)=2 true → counter=1, x=2. Next: (2-1)=1 true → counter=2, x=1. Next: (1-1)=0 false → exit. Final counter=2. This is correct.