Computer Applications

How many times will the following loop execute?

public static void main(String args[])
{
    int sum = 0;
    for (int i = 10; i > 5; i++)
    {
        sum += i;
    }
    System.out.println(sum);
}
  1. 5
  2. 0
  3. 15
  4. Infinite loop

Java Iterative Stmts

7 Likes

Answer

Infinite loop

Reason — The loop will run for infinite times as i is initialised as 10 and the update expression i++ increments i after each iteration. Hence, the test expression i > 5 will always remain true.

Answered By

5 Likes


Related Questions