Computer Applications

To execute a loop 10 times, which of the following is correct?

  1. for (int i=11;i<=30;i+=2)
  2. for (int i=11;i<=30;i+=3)
  3. for (int i=11;i<20;i++)
  4. for (int i=11;i<=21;i++)

Java Iterative Stmts

ICSE Sp 2025

7 Likes

Answer

for (int i=11;i<=30;i+=2)

Reason — To execute a loop 10 times, the total number of iterations should match 10, based on the condition and increment values. Let's analyse each option:

1. for (int i = 11; i <= 30; i += 2)

  • The loop starts at 11 and increments by 2.
  • The sequence is: 11, 13, 15, 17, 19, 21, 23, 25, 27, 29.
  • This loop executes 10 times.

2. for (int i = 11; i <= 30; i += 3)

  • The loop starts at 11 and increments by 3.
  • The sequence is: 11, 14, 17, 20, 23, 26, 29.
  • This loop executes only 7 times, so it is incorrect.

3. for (int i = 11; i < 20; i++)

  • The loop starts at 11 and increments by 1.
  • The sequence is: 11, 12, 13, 14, 15, 16, 17, 18, 19.
  • This loop executes only 9 times, so it is incorrect.

4. for (int i = 11; i <= 21; i++)

  • The loop starts at 11 and increments by 1.
  • The sequence is: 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21.
  • This loop executes 11 times, so it is incorrect.

Answered By

4 Likes


Related Questions