How do I use the continue statement?

The continue statement has two forms, the unlabeled and labeled continue statement. The first example shows you how to use the unlabeled continue statement while the second example shows you how to use the labeled continue statement.

package org.kodejava.basic;

public class ContinueDemo {
    public static void main(String[] args) {
        int[] numbers = {5, 11, 3, 9, 12, 15, 4, 7, 6, 17};
        int counter = 0;

        for (int number : numbers) {
            // When number is greater or equals to 10 skip the
            // current loop and continue to the next loop because
            // we only interested to count number less than 10.
            if (number >= 10) {
                continue;
            }

            counter++;
        }

        System.out.println("Found " + counter + " numbers less than 10.");

        // The example below used a labeled continue statement. In the
        // loop below we sum the number in the array until reminder of
        // the number divided by 2 equals to zero. We skip to the next
        // array if the reminder is 0.
        int[][] values = {
                {8, 2, 1},
                {3, 3},
                {3, 4, 5},
                {5, 4},
                {6, 5, 2}};

        int total = 0;

        outer:
        for (int[] value : values) {
            for (int item : value) {
                if (item % 2 == 0) {
                    continue outer;
                }
                total += item;
            }
        }

        System.out.println("Total = " + total);
    }
}

The output of the code snippet:

Found 6 numbers less than 10.
Total = 14

How do I use the break statement?

The break statement has two forms, the unlabeled and labeled break. On the example below you see the first example as the unlabeled break. This type of break statement will terminate the innermost loop such as the for, while and do-while loop.

On the second example you’ll see the labeled break. We have two loops, the infinite while and the inner for loop. Using the labeled break we can terminate the outermost loop. In the for loop when the value of y equals to 5 then it will break to the start: label which will continue the program execution to the line after the while loop.

package org.kodejava.basic;

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

        int index;
        boolean found = false;

        int search = 7;
        for (index = 0; index < numbers.length; index++) {
            if (numbers[index] == search) {
                found = true;
                break;
            }
        }

        if (found) {
            System.out.println(search + " found at index " + index);
        } else {
            System.out.println(search + " was not found.");
        }

        start:
        while (true) {
            for (int y = 0; y < 10; y++) {
                System.out.print("y = " + y + "; ");
                if (y == 5) {
                    System.out.println();
                    break start;
                }
            }
        }
    }
}

How do I use the for loop statement?

The for loop can be used to iterate over a range of values. For instance if you want to iterate from 0 to 10 or if you want to iterate through all the items of an array. Below you’ll see two forms of a for loop. The first one is the general form of a for loop and the second one is an enhanced for loop that also known as the for..each loop.

The general form of for loop consists of three parts:

for (initialization; termination; increment) {
    ....
}
  • The initialization: it initializes the loop, it executed once at the beginning of the loop.
  • The termination: the loop executes as long as the termination evaluates to true.
  • The increment: it executed at the end of every loop, the expression can be either an increment or decrement.
package org.kodejava.basic;

public class ForDemo {
    public static void main(String[] args) {
        // Do a loop from 0 to 10.
        for (int i = 0; i <= 10; i++) {
            System.out.println("i = " + i);
        }

        // Loop through all the array items.
        int[] numbers = new int[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        for (int number : numbers) {
            System.out.println("number = " + number);
        }
    }
}

The result of the program is:

i = 0
i = 1
i = 2
i = 3
i = 4
i = 5
i = 6
i = 7
i = 8
i = 9
i = 10
number = 0
number = 1
number = 2
number = 3
number = 4
number = 5
number = 6
number = 7
number = 8
number = 9
number = 10

How do I use the do-while loop statement?

There is also a do-while loop in the Java programming language. Instead of evaluating the expression at the beginning like the while loop does the do-while loop evaluates its expression at the end of the loop. Due to this the loop executes at least once during the program execution.

package org.kodejava.basic;

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

        // The do-while statement executes at least once because
        // the expression is checked at the end of the loop
        // process.
        do {
            // This block will be executed while `i` is smaller or
            // equals to 10.
            System.out.println(i);
            i++;
        } while (i <= 10);
    }
}

The program prints the following result:

0
1
2
3
4
5
6
7
8
9
10

How do I use the while loop statement?

The code below demonstrate how to use the while loop statement. The while statement will check if its expression evaluates to true and execute the while statement block.

The program below will execute while the countDown value is bigger or equals to zero.

package org.kodejava.basic;

public class WhileDemo {
    public static void main(String[] args) {
        // Start the count-down from 10
        int countDown = 10;

        // Do the count-down process while the value of
        // countDown is bigger or equals to zero.
        while (countDown >= 0) {
            System.out.println(countDown);
            countDown--;

            try {
                // Adds one-second delay.
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}