Which type of loop is best known for using an index or counter?

In this post, we will discuss one more important MCQ (Multiple Choice Question) on Java Loops.

Which type of loop is best known for using an index or counter?

a) do-while loop

b) for(traditional)

c) for-each

d) while

Answer:

b) for(traditional)

Explanation:

The "for" loop is best known for using an index or counter. It is typically used when you know how many times you want the loop to execute. 

Here's an example in Java:
for (int i = 0; i < 10; i++) {
    System.out.println(i);
}
In this example, i is the loop counter, and the loop will continue as long as i is less than 10. Each iteration of the loop will increase i by 1 (i++).

Comments