June 2025
Intermediate to advanced
1093 pages
33h 24m
English
This much rarer variant of the while loop has the condition at the end:
do Statement while( Condition )
Here, the Statement is executed first and then the Condition is checked, as you can see in the flowchart in Figure 8.4. The check occurs after each iteration. This means that the statement is executed at least once.
// https://godbolt.org/z/6hh5KhExM#include <iostream> // cin#include <string>int main() { std::string line; do { // execute getline at least once std::getline(std::cin, line); if(!std::cin) break; // error or end of file } while(line != "quit"); // end on specific input}
Listing 8.13 The body of a “do-while” loop is executed at least once.
You should not use this variant of the while loop with
Read now
Unlock full access