if Statements and Control Flow

AP Computer Science A· difficulty 2/5

if (x > 0)
    x--;
    y++;

Why is this misleading?

  • A

    Both statements depend on the if

  • B

    x-- never executes

  • C

    Only x-- depends on the if; y++ always runs

    check_circle
  • D

    Compile error due to missing braces

Explanation

Without braces, only the next single statement is part of the if. Indentation is ignored, so y++ runs unconditionally.

Want 10 more like this — adaptive to your weak spots?

Related questions