KnowledgeBoat Logo
|

Computer Applications

Identify which of the following leads to an infinite loop.

  1. for(i = 10; i != 0; i--)
  2. for(i = 3; i <= 30; i += 3)
  3. for(i = 1; i >= 1; i++)
  4. for(i = 1; i >= 0; i--)

Java Iterative Stmts

2 Likes

Answer

for(i = 1; i >= 1; i++)

Reason — In the loop for(i = 1; i >= 1; i++), the condition i >= 1 will always be true because i starts at 1 and increases by 1 in each iteration. Since i is always greater than or equal to 1, the loop condition never becomes false. Therefore, the loop runs infinitely, causing an infinite loop.

Answered By

2 Likes


Related Questions