◐ Shell
clean mode source ↗

for loop - cppreference.com

De cppreference.com

<metanoindex/>

Executa um loop.

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.

Sintaxe

for ( init_expression ; cond_expression ; iteration_expression ) loop_statement

Explicação

A sintaxe acima produz o equivalente a codificar:

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 ;
}

}

O init_expression é executado antes da execução do loop. O cond_expression deve avaliar em valor, conversível a bool. Ela é avaliada antes de cada iteração do loop. O loop continua somente se seu valor é true. O loop_statement é executado em cada iteração, após o que é executada iteration_expression.

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.

Palavras-chave

for

Exemplo

#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 << " ";
    }
}

Saída:

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