Java do while Loop Example

Java do-while Loop Example demonstrates how to use do-while in Java with an example.

do-while Loop Syntax

do {
// body of a loop
} while (condition);
Each iteration of the do-while loop first executes the body of the loop and then evaluates the conditional expression. If this expression is true, the loop will repeat. Otherwise, the loop terminates. As with all of Java’s loops, a condition must be a Boolean expression.

Simple do-while Loop Example

package net.javaguides.corejava.controlstatements.loops;

public class DoWhileLoopExample {
    public static void main(String args[]) {
        int n = 10;
        do {
            System.out.println("tick " + n);
            n--;
        } while (n > 0);
    }
}
Output:
tick 10
tick 9
tick 8
tick 7
tick 6
tick 5
tick 4
tick 3
tick 2
tick 1

Comments