Apex Developer Guide
Release Notes
Conditional (If-Else) Statements
Switch Statements
Do-While Loops
While Loops
Apex Reference
Previous PDF Versions
DID THIS ARTICLE SOLVE YOUR ISSUE?
Let us know so we can improve!
Let us know so we can improve!
The Apex do-while loop repeatedly executes a block of code as long as a particular Boolean condition remains true. In this syntax, code_block is a placeholder value. The syntax is:
1do {
2 code_block
3} while (condition);Curly braces ({}) are always required around a code_block.
Note
As in Java, the Apex do-while loop does not check the Boolean condition statement until after the first loop is executed. Consequently, the code block always runs at least once.
As an example, the following code outputs the numbers 1–10 into the debug log:
1Integer count = 1;
2
3do {
4 System.debug(count);
5 count++;
6} while (count < 11);