Java For Loop Examples

Welcome to The Coding College! In this tutorial, we’ll look at practical examples of the for loop in Java. The for loop is one of the most commonly used loops, allowing you to iterate over a block of code multiple times based on a condition.

Basic Syntax of a For Loop

for (initialization; condition; update) {
    // Code to be executed
}
  • Initialization: Sets the starting point of the loop.
  • Condition: Specifies the stopping point; the loop runs as long as this is true.
  • Update: Defines how the loop variable changes after each iteration.

Example 1: Printing Numbers

Code

public class PrintNumbers {
    public static void main(String[] args) {
        for (int i = 1; i <= 10; i++) {
            System.out.println(i);
        }
    }
}

Output

1  
2  
3  
4  
5  
6  
7  
8  
9  
10

Example 2: Summing Numbers

Code

public class SumNumbers {
    public static void main(String[] args) {
        int sum = 0;

        for (int i = 1; i <= 5; i++) {
            sum += i; // Add the current number to the sum
        }

        System.out.println("Sum: " + sum);
    }
}

Output

Sum: 15

Example 3: Printing Even Numbers

Code

public class EvenNumbers {
    public static void main(String[] args) {
        for (int i = 2; i <= 20; i += 2) {
            System.out.println(i);
        }
    }
}

Output

2  
4  
6  
8  
10  
12  
14  
16  
18  
20

Example 4: Reverse Counting

Code

public class ReverseCount {
    public static void main(String[] args) {
        for (int i = 10; i >= 1; i--) {
            System.out.println(i);
        }
    }
}

Output

10  
9  
8  
7  
6  
5  
4  
3  
2  
1

Example 5: Multiplication Table

Code

public class MultiplicationTable {
    public static void main(String[] args) {
        int number = 5;

        for (int i = 1; i <= 10; i++) {
            System.out.println(number + " x " + i + " = " + (number * i));
        }
    }
}

Output

5 x 1 = 5  
5 x 2 = 10  
5 x 3 = 15  
5 x 4 = 20  
5 x 5 = 25  
5 x 6 = 30  
5 x 7 = 35  
5 x 8 = 40  
5 x 9 = 45  
5 x 10 = 50

Example 6: Factorial Calculation

Code

public class Factorial {
    public static void main(String[] args) {
        int number = 5;
        int factorial = 1;

        for (int i = 1; i <= number; i++) {
            factorial *= i;
        }

        System.out.println("Factorial of " + number + " is " + factorial);
    }
}

Output

Factorial of 5 is 120

Example 7: Nested For Loop for Patterns

Code: Printing a Star Triangle

public class StarPattern {
    public static void main(String[] args) {
        for (int i = 1; i <= 5; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print("* ");
            }
            System.out.println();
        }
    }
}

Output

*  
* *  
* * *  
* * * *  
* * * * *

Example 8: Iterating Over an Array

Code

public class ArrayIteration {
    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 at index " + i + ": " + numbers[i]);
        }
    }
}

Output

Element at index 0: 10  
Element at index 1: 20  
Element at index 2: 30  
Element at index 3: 40  
Element at index 4: 50

Example 9: Checking Prime Numbers

Code

public class PrimeCheck {
    public static void main(String[] args) {
        int number = 29;
        boolean isPrime = true;

        for (int i = 2; i <= number / 2; i++) {
            if (number % i == 0) {
                isPrime = false;
                break;
            }
        }

        if (isPrime) {
            System.out.println(number + " is a prime number.");
        } else {
            System.out.println(number + " is not a prime number.");
        }
    }
}

Output

29 is a prime number.

Example 10: Skipping Iterations (Using continue)

Code

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

Output

1  
2  
3  
4  
6  
7  
8  
9  
10

Practice Problems

  1. Write a program to print all odd numbers between 1 and 50 using a for loop.
  2. Create a program to calculate the sum of all numbers divisible by 3 from 1 to 100.
  3. Generate a pyramid pattern using nested loops.
  4. Write a program to find the smallest number in an array using a for loop.

For more Java tutorials and practical examples, visit The Coding College. Keep learning and coding! 🚀

Leave a Comment