while Loops

AP Computer Science A· difficulty 3/5

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

What is printed?

  • A

    55

  • B

    10

    check_circle
  • C

    15

  • D

    5

Explanation

When i = 5, break exits the loop before adding 5. Sum = 1+2+3+4 = 10.

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

Related questions