Loop Control in Java Control Statements

Loop Control

Java has the for, while, and do-while statements, as well as the for~each statement added in J2EE 5.0. Loop statements repeat specified statements until a condition is satisfied. Depending on how the condition is specified, for, while, do-while, and for~each statements are used separately. If the statement to repeat is a single statement, it does not have to be placed in a block. If there is more than one statement, braces ({ }) must be used to define a block.

for Statement

The for statement repeatedly executes a for block while a condition is satisfied, starting from a given initial value.

for (initial value; condition; increment/decrement) {
   repeated statement;
}

When the for statement starts, the initial value part is executed only once. Then the condition expression is executed, and this condition expression must be a boolean expression. When the condition expression becomes false, the loop ends. The increment/decrement part contains an expression that increases or decreases the loop control variable. After the first condition expression is executed, the statement is executed. Then the increment/decrement part is executed, the condition expression is checked again, and the statement is repeated.

The following example calculates the sum from 1 to 100.

package com.devkuma.tutorial.control.statement;

public class For1 {

    public static void main(String[] args) {

        int sum = 0;

        for (int i = 1; i <= 100; i++) {
            sum += i;
        }

        System.out.println("sum=" + sum);
    }
}

If you want to list one or more statements in the initial value, condition expression, or increment/decrement part of a for statement, you can declare them by using commas.

The following two examples print the same result.

package com.devkuma.tutorial.control.statement;

public class For2 {

    public static void main(String[] args) {

        int a, b;
        b = 4;

        for (a = 1; a < b; a++) {
            System.out.println("a=" + a + ", b=" + b);
            b--;
        }
    }
}
package com.devkuma.tutorial.control.statement;

public class For3 {

    public static void main(String[] args) {

        int a, b;

        for (a = 1, b = 4; a < b; a++, b--) {
            System.out.println("a=" + a + ", b=" + b);
        }
    }
}

for ~ each Statement

JDK 1.5 added another form of the for statement, called the for~each statement.

The for~each statement repeatedly executes for each item that exists in collection-type data.

for (variable : collection) {
    repeated statement;
}

The following example uses array and collection-type data.

package com.devkuma.tutorial.control.statement;

import java.util.ArrayList;
import java.util.List;

public class ForEach {

    public static void main(String[] args) {

        String array[] = { "A", "B", "C" };
        for (String a : array) {
            System.out.println("a=" + a);
        }

        List<String> list = new ArrayList<String>();
        list.add("A");
        list.add("B");
        list.add("C");
        for (String l : list) {
            System.out.println("l=" + l);
        }
    }
}

while Statement

The while statement repeatedly executes the while block while the condition is true.

while (condition) {
    repeated statement;     
}

An example using the while statement is shown below.

package com.devkuma.tutorial.control.statement;

public class While {

    public static void main(String[] args) {

        int sum = 0;
        int i = 1;
        while (i <= 100) {
            sum += i;
            i++;
        }

        System.out.println("sum=" + sum);
    }
}

do ~ while Statement

This loop can be executed at least once.

do {
    repeated statement;  
} while (condition)

An example using the do ~ while statement is shown below.

package com.devkuma.tutorial.control.statement;

public class DoWhile {

    public static void main(String[] args) {

        int sum = 0;
        int i = 1;
        do {
            sum += i;
            i++;
        } while (i <= 100);

        System.out.println("sum=" + sum);
    }
}

The difference between a while statement and a do ~ while statement is when the condition expression is checked.