Java Control Statements
- Java if Statement
- Java Nested if Statement
- Java if-else-if Statement
- Java Switch Case
- Java Switch with String
- Java Nested Switch
- Java Simple for Loop
- Java for each Loop
- Java Nested for Loops
- Java while loop
- Java continue Statement with for Loop
- Java continue in do-while Loop
- Java break to Exit a for Loop
- Java break with Nested Loops
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