Loops are control flow statements that allow code to be executed repeatedly based on a given condition.
The do while
loop is a variant of the while
loop that executes the code block once before checking the condition. Then it will repeat the loop as long as the condition is true.
Here’s the basic syntax for a do while loop:
do {
// body of the loop
}
while (condition);
Note that the test of the termination condition is made after each execution of the loop. This means that the loop will always be executed at least once, even if the condition is false in the beginning.
This is in contrast to the normal while
loop, where the condition is tested before the loop, and an execution of the code block is not guaranteed.
Now here’s a regular while loop:
while (condition) {
// body of the loop
}
#cplusplus #developer