KnowledgeBoat Logo
|

Computer Applications

State the type of loop in the given program segment:

for (int i = 5; i != 0; i -= 2)
    System.out.println(i);
  1. finite
  2. infinite
  3. null
  4. fixed

Java Iterative Stmts

ICSE 2023

38 Likes

Answer

infinite

Reason — The given loop is an example of infinite loop as for each consecutive iteration of for loop, the value of i will be updates as follows:

IterationValue of iRemark
15Initial value of i = 5
23i = 5 - 2 = 3
31i = 3 - 2 = 1
4-1i = 1 - 2 = -1
5-3i = -1 - 2 = -3 and so on…

Since i will never be '0', the loop will execute infinitely.

Answered By

20 Likes


Related Questions