while Loops

AP Computer Science A· difficulty 3/5

int sum = 0;
for (int i = 1; i <= 5; i++) {
  if (i % 2 == 0) {
    continue;
  }
  sum += i;
}
System.out.println(sum);

What is printed?

  • A

    15

  • B

    9

    check_circle
  • C

    6

  • D

    5

Explanation

continue skips the rest of the iteration when i is even. Only odd values 1, 3, 5 are added: 1+3+5 = 9.

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

Related questions