◐ Shell
clean mode source ↗

while loop — cppreference.com

De cppreference.com

<metanoindex/>

Exécute une boucle .

Original:

Executes a loop.

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Utilisé lorsque le code doit être exécuté plusieurs fois tant qu'une condition est présent .

Original:

Used where code needs to be executed several times while some condition is present.

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Syntaxe

while ( cond_expression ) loop_statement

Explication

cond_expression doit être une expression dont le résultat est convertible en bool. Il est évaluée avant chaque exécution de loop_statement, qui est exécutée seulement si le cond_expression évalue à true .

Original:

cond_expression shall be an expression, whose result is convertible to bool. It is evaluated before each execution of loop_statement, which is only executed if the cond_expression evaluates to true.

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Mots-clés

while

Exemple

#include <iostream>

int main()
{
    int i = 0;
    while (i < 10) i++;
    
    std::cout << i << '\n';
    
    int j = 2;
    while (j < 9) {
        std::cout << j << " ";
        j += 2;
    }
}

Résultat :