WikiGalaxy

Personalize

Java For Loop

Basic For Loop:

The basic for loop in Java is used to iterate over a range of values, executing a block of code multiple times. It consists of three parts: initialization, condition, and increment/decrement.


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

Console Output:

Iteration: 0

Iteration: 1

Iteration: 2

Iteration: 3

Iteration: 4

For Loop with Array

Iterating Over an Array:

For loops are commonly used to iterate over arrays, allowing you to access each element by its index.


public class ArrayLoopExample {
    public static void main(String[] args) {
        int[] numbers = {10, 20, 30, 40, 50};
        for (int i = 0; i < numbers.length; i++) {
            System.out.println("Element: " + numbers[i]);
        }
    }
}
    

Console Output:

Element: 10

Element: 20

Element: 30

Element: 40

Element: 50

Enhanced For Loop

Using Enhanced For Loop:

The enhanced for loop, also known as the for-each loop, simplifies the iteration over arrays and collections by eliminating the need for index management.


public class EnhancedForLoopExample {
    public static void main(String[] args) {
        String[] fruits = {"Apple", "Banana", "Cherry"};
        for (String fruit : fruits) {
            System.out.println("Fruit: " + fruit);
        }
    }
}
    

Console Output:

Fruit: Apple

Fruit: Banana

Fruit: Cherry

Nested For Loops

Working with Nested Loops:

Nested for loops are used when you need to perform repeated actions within another loop. They are often used for multi-dimensional data structures.


public class NestedLoopExample {
    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);
            }
        }
    }
}
    

Console Output:

i: 1, j: 1

i: 1, j: 2

i: 1, j: 3

i: 2, j: 1

i: 2, j: 2

i: 2, j: 3

i: 3, j: 1

i: 3, j: 2

i: 3, j: 3

For Loop with Break

Using Break in For Loop:

The break statement is used to exit a loop prematurely when a specific condition is met.


public class BreakExample {
    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            if (i == 5) {
                break;
            }
            System.out.println("Number: " + i);
        }
    }
}
    

Console Output:

Number: 0

Number: 1

Number: 2

Number: 3

Number: 4

For Loop with Continue

Using Continue in For Loop:

The continue statement skips the current iteration of the loop and proceeds to the next iteration.


public class ContinueExample {
    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            if (i % 2 == 0) {
                continue;
            }
            System.out.println("Odd Number: " + i);
        }
    }
}
    

Console Output:

Odd Number: 1

Odd Number: 3

Odd Number: 5

Odd Number: 7

Odd Number: 9

For Loop with Decrement

Decrementing in For Loop:

For loops can also decrement the loop variable, which is useful for counting downwards.


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

Console Output:

Countdown: 5

Countdown: 4

Countdown: 3

Countdown: 2

Countdown: 1

For Loop with Multiple Variables

Using Multiple Variables in For Loop:

Java allows the use of multiple variables in a single for loop, which can be useful for iterating over multiple sequences simultaneously.


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

Console Output:

i: 0, j: 10

i: 1, j: 9

i: 2, j: 8

i: 3, j: 7

i: 4, j: 6

For Loop with Complex Condition

Complex Conditions in For Loop:

A for loop can have complex conditions that involve multiple logical operators, allowing for more sophisticated iteration control.


public class ComplexConditionExample {
    public static void main(String[] args) {
        for (int i = 0; i < 10 && i % 2 == 0; i++) {
            System.out.println("Even Number: " + i);
        }
    }
}
    

Console Output:

Even Number: 0

logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025