KnowledgeBoat Logo
|

Computer Applications

Select the infinite loop:

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

Java Iterative Stmts

7 Likes

Answer

for(int i = 2; i != 0; i -= 3)

Reason — The loop for(int i = 2; i != 0; i -= 3) is an infinite loop because the value of i starts at 2 and decreases by 3 in every iteration. Since i will never become 0 (it will take values like 2, -1, -4, -7, etc.), the condition i != 0 will always be true. Therefore, the loop will continue to run forever.

Answered By

4 Likes


Related Questions