◐ Shell
clean mode source ↗

for 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.

Syntaxe

for ( init_expression ; cond_expression ; iteration_expression ) loop_statement

Explication

La syntaxe ci-dessus produit code équivalent à:

Original:

The above syntax produces code equivalent to:

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

{
init_expression ;
while ( cond_exression ) {
loop_statement
iteration_expression ;
}

}

La init_expression est exécutée avant l'exécution de la boucle. Le cond_expression doit évaluer à la valeur convertible en bool. Il est évaluée avant chaque itération de la boucle. La boucle continue seulement si sa valeur est true. La loop_statement est exécuté à chaque itération, après quoi iteration_expression est exécutée .

Original:

The init_expression is executed before the execution of the loop. The cond_expression shall evaluate to value, convertible to bool. It is evaluated before each iteration of the loop. The loop continues only if its value is true. The loop_statement is executed on each iteration, after which iteration_expression is executed.

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

for

Exemple

#include <iostream>

int main() 
{
    for (int i = 0; i < 10; i++) {
        std::cout << i << " ";
    }

    std::cout << '\n';
    
    for (int j = 2; j < 9; j = j + 2) {
        std::cout << j << " ";
    }
}

Résultat :

0 1 2 3 4 5 6 7 8 9
2 4 6 8