Java for Loop

The for loop in Java is a control flow statement that is used to repeatedly execute a block of code for a specific number of iterations. It is particularly useful when you know beforehand how many times the loop needs to run.

Syntax:


for (initialization; condition; increment/decrement) {
    // Code to be executed
}

Explanation

1. Initialization: This is executed only once at the beginning of the loop. It is typically used to declare and initialize the loop control variable.

2. Condition: This is evaluated before each iteration. If the condition evaluates to true, the loop continues; otherwise, the loop stops.

3. Update: This is executed after each iteration and is usually used to update the loop control variable.

Example: print value from 1 to 5 through for loop


public class ForLoopExample {
    public static void main(String[] args) {
        for (int i = 1; i <= 5; i++) {
            System.out.println("Count: " + i);
        }
    }
}

Output:

Count: 1
Count: 2
Count: 3
Count: 4
Count: 5

Working Process:

1. Initialization: int i = 1; sets the loop control variable i to 1.

2. Condition: i <= 5; checks if i is less than or equal to 5.

3. Body Execution: The code inside the loop (System.out.println) is executed.

4. Update: i++ increments i by 1 after each iteration.

Reverse Loop

Loops can count down instead of up.


public class ReverseForLoop {
    public static void main(String[] args) {
        for (int i = 5; i >= 1; i--) {
            System.out.println("Count: " + i);
        }
    }
}

Output:

Count: 5
Count: 4
Count: 3
Count: 2
Count: 1

Nested for Loop

You can use one for loop inside another to handle multidimensional scenarios, like printing patterns.


public class NestedForLoop {
    public static void main(String[] args) {
        for (int i = 1; i <= 3; i++) {
            for (int j = 1; j <= 3; j++) {
                System.out.println("i: " + i + ", j: " + j);
            }
        }
    }
}

Output:

i: 1, j: 1
i: 1, j: 2
i: 1, j: 3
i: 2, j: 1
...
i: 3, j: 3

Enhanced for Loop (for-each Loop)

Used to iterate over arrays or collections.

Example with Arrays:


public class ForEachLoop {
    public static void main(String[] args) {
        int[] numbers = {10, 20, 30, 40, 50};
        for (int num : numbers) {
            System.out.println(num);
        }
    }
}

Output:

10
20
30
40
50

Skipping a Loop

Skip the current iteration and move to the next.


public class ContinueExample {
    public static void main(String[] args) {
        for (int i = 1; i <= 5; i++) {
            if (i == 3) continue; // Skip when i is 3
            System.out.println("Count: " + i);
        }
    }
}

Output:

Count: 1
Count: 2
Count: 4
Count: 5

Using break

Exit the loop prematurely.


public class BreakExample {
    public static void main(String[] args) {
        for (int i = 1; i <= 5; i++) {
            if (i == 3) break; // Stop when i is 3
            System.out.println("Count: " + i);
        }
    }
}

Output:

Count: 1
Count: 2

Infinite Loop

A for loop can run indefinitely if no stopping condition is provided.


public class InfiniteLoop {
    public static void main(String[] args) {
        for (;;) {
            System.out.println("This is an infinite loop.");
        }
    }
}