Notes
Loops can execute a block of code as long as a specified condition is reached.
The while
loop loops through a block of code as long as a specified condition is true:
Integer i = 0;
while (i < 5) {
System.debug(i);
i++;
}
Output: 0 1 2 3 4 5
The do
/while
loop is a variant of the while loop. This loop will execute the code block once, before checking if the condition is true, then it will repeat the loop as long as the condition is true.
Integer i = 0;
do {
System.debug(i);
i++;
}
while (i < 5);