Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

do — a loop with its test at the end

The do keyword starts a do-while loop: its body executes before the controlling expression is tested. The loop repeats while that expression evaluates to true. The trailing semicolon is required.

Version: C++23; repository compiler flag, production runtime not attested

Syntax

do statement while (expression);

do starts a loop with the syntax do statement while (expression);. Its body is one statement, often a block { ... }. The semicolon after while (expression) is a required part of the syntax.

Unlike while, a do loop tests its expression after executing the body, so the body executes at least once when the loop is entered. The expression is contextually converted to bool: true causes another iteration, and false ends the loop. The parentheses require an expression, not a variable declaration.

A continue statement targeting this loop skips the remainder of the current body execution and proceeds to the expression test. It does not unconditionally start another iteration.

In the example, count increases before being printed. The program prints 1, 2, and 3, each on its own line; then count < 3 yields false. The supplied source is an evolving working draft, not a frozen C++23 publication.

Examples

#include <iostream>

int main() {
    int count = 0;
    do {
        ++count;
        std::cout << count << '\n';
    } while (count < 3);
}

Result

1
2
3

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources