if Statements and Control Flow

AP Computer Science A· difficulty 3/5

<code>if (x > 0) y = 1; z = 2;</code> — if x = -5, what happens?

  • A

    Both skipped

  • B

    y stays unchanged; z = 2 (z is OUTSIDE the if)

    check_circle
  • C

    y = 1; z = 2

  • D

    Compile error

Explanation

Without braces, only the next statement is in the if. <code>z = 2;</code> always executes.

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

Related questions