Multiple choice

Give the output of the following: int n=10; while (true) {if (n<5) continue; n=n-2;} System.out.println(n);

  1. 5

  2. 10

  3. 2

  4. Infinite loop

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

The loop is 'while(true)'. If n is 10, it is not < 5, so it skips the continue and sets n = 10-2 = 8. Then n=6, then n=4. When n=4, it hits the continue, skipping the n=n-2 line. This creates an infinite loop where n remains 4.