For Loop

In Java, the for loop is a control flow statement that allows you to repeatedly execute a block of code a specified number of times. It is particularly useful when you know the number of iterations you need in advance.

  1. initialization - assign value to a variable.
  2. condition - check condition.
  3. update - change the value of variable.

Infinite For Loop

Infinite for loop never stops.

Nested For Loop

Nested for loop is for loop within for loop.

For each Loop

In Java, the "for-each" loop, also known as the enhanced for loop, provides a convenient way to iterate through elements in an array or collection (such as ArrayList, List, Set, or Map) without the need for explicit index-based iteration.

Practice Exercises

Complete these exercises to reinforce your learning and earn XP

Sign in to track your progress and earn XP!
Exercise 1 of 2Easy

What will be the output?

15 XP~4 min
for(int i = 0; i < 3; i++) {
    System.out.print("%d ", i);
}
Exercise 2 of 2Easy

To exit a loop immediately, we use the ___ statement.

10 XP~2 min