Java do-while Loop
In this page:
do {
// runs at least once
} while (condition);
Basic do-while Loop
A do-while loop runs its body first and only checks the condition afterward, at the bottom -- the opposite order from a regular while loop, which tests the condition before ever entering the body.
Example: Basic do-while Loop
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Declare i and set it to 0
int i = 0;
// do-while: run the body once, then repeat while the condition is true
do {
// Print a line to the console
System.out.println(i);
i++;
} while (i < 3);
}
}
Login to try C/C++/Java/PHP code in the editor
Guaranteeing One Run
This ordering guarantees the loop body executes at least once even if the condition would have been false from the very start, which a standard while loop can never guarantee.
Example: Guaranteeing One Run
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Declare i and set it to 10
int i = 10;
// do-while: run the body once, then repeat while the condition is true
do {
// Print a line to the console
System.out.println("Runs once: " + i);
} while (i < 5);
}
}
Login to try C/C++/Java/PHP code in the editor
Incrementing Variables inside Loop
You must modify the variable checked in your loop condition somewhere inside the do-while block, or the loop keeps repeating forever since the condition never has a chance to become false.
Example: Incrementing Variables inside Loop
public class Main {
public static void main(String[] args) {
int i = 0;
do {
System.out.println(i);
i++; // without this, the loop never ends
} while (i < 3);
}
}
Login to try C/C++/Java/PHP code in the editor
Simulated Interactive Runs
Just like any loop, you must update the variable the condition depends on somewhere inside the do-while body -- if that variable never changes, the loop will run forever once it starts.
Example: Simulated Interactive Runs
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Declare attempts and set it to 0
int attempts = 0;
// do-while: run the body once, then repeat while the condition is true
do {
attempts++;
// Print a line to the console
System.out.println("Attempt " + attempts);
} while (attempts < 3);
}
}
Login to try C/C++/Java/PHP code in the editor
Nested do-while Loops
Nesting a do-while inside another do-while lets you build structures like a grid where each outer iteration always processes at least one full pass of the inner loop, guaranteed.
Example: Nested do-while Loops
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Declare row and set it to 1
int row = 1;
// do-while: run the body once, then repeat while the condition is true
do {
// Declare col and set it to 1
int col = 1;
// do-while: run the body once, then repeat while the condition is true
do {
// Print to the console without a newline
System.out.print(col + " ");
col++;
} while (col <= 3);
// Print a line to the console
System.out.println();
row++;
} while (row <= 2);
}
}
Login to try C/C++/Java/PHP code in the editor
- Forgetting the semicolon after
while (condition)in a do-while, which is a compile error. - Expecting the body to be skipped when the condition is false, when do-while always runs once.
- Declaring a variable inside the body and using it in the
whilecondition, where it is out of scope.
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: