Introduction to Java: For Loop

Posted by Hayri Cicek on June 10, 2019


In this tutorial, you will learn how to create a for loop.
A loop is used to repeat a specific block of code until the condition is met.

Example 1:
Create a new file called PrintNumbersFrom0To10.java and type in the following :


1 public class PrintNumbersFrom0To10 {
2    public static void main(String[] args) {
3        int num = 10;
4
5        for (int i = 1; i <= num; i++) {
6            System.out.println("Num: " + i);
7        }
8    }
9 }

At line 3, we declare the variable num and initialize it to 10.
Inside our for loop, we declare another variable called i and initialize it to 0 and test it against our num variable.

$ javac PrintNumbersFrom0To10.java
$ java PrintNumbersFrom0To10


Output:

Num: 0
Num: 1
Num: 2
Num: 3
Num: 4
Num: 5
Num: 6
Num: 7
Num: 8
Num: 9
Num: 10


foreach-loop statement

In this example, we will use the foreach-loop statement to iterate through each element of the array.
Create a new file called PrintNamesUsingForEachStatement.java and add the following:

public class PrintNamesUsingForEachStatement {
    public static void main(String[] args) {
        String[] names = new String[5];

        names[0] = "Hayri";
        names[1] = "Kodnito";
        names[2] = "Java";
        names[3] = "MicroProfile";
        names[4] = "Spring Boot";

        System.out.println("Length of names " + names.length);

        for (String name : names) {
            System.out.println("Hello " + name);
        }
    }
}


Here we create a new array of Strings and we assign a few names to the array.
We print out the size of the array by using the length property and now we use the forEach loop to iterate through each element of the array and print it out to the terminal.

Output:

$ javac PrintNamesUsingForEachStatement.java
$ java PrintNamesUsingForEachStatement

Length of names 5
Hello Hayri
Hello Kodnito
Hello Java
Hello MicroProfile
Hello Spring Boot


In this example, we have an array of numbers and are using forEach statement to output each number to the terminal.

public class PrintNumbersUsingForEachStatement {
    public static void main(String[] args) {
        int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

        for (int num : numbers) {
            System.out.println("number " + num);
        }
    }
}


Output:

$ javac PrintNumbersUsingForEachStatement.java
$ java PrintNumbersUsingForEachStatement

number 1
number 2
number 3
number 4
number 5
number 6
number 7
number 8
number 9
number 10


Break
With break statement, we can stop a for-loop at any time based on the condition.

public class BreakForLoop {
    public static void main(String[] args) {
        int[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

        for (int i : numbers) {
            if (i == 5) {
                break;
            }

            System.out.println("num " + i);
        }
    }
}


Output:

$ javac BreakForLoop.java
$ java BreakForLoop

num 1
num 2
num 3
num 4


Continue
Continue keyword stops the current iteration and move to the next one.

public class ContinueForLoop {
    public static void main(String[] args) {
        int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

        for (int i : numbers) {
            if (i == 3) {
                continue;
            }

            System.out.println("number " + i);
        }
    }
}


Output

$ javac ContinueForLoop.java
$ java ContinueForLoop

number 1
number 2
number 4
number 5
number 6
number 7
number 8
number 9
number 10


Share this: