◐ Shell
clean mode source ↗

for loop - cppreference.com

Da cppreference.com.

Questa pagina è stata tradotta in modo automatico dalla versione in ineglese della wiki usando Google Translate.

La traduzione potrebbe contenere errori e termini strani. Muovi il puntatore sopra al testo per vedere la versione originale. Puoi aiutarci a correggere gli gli errori. Per ulteriori istruzioni clicca qui.

Click here for the English version of this page

<metanoindex/>

Esegue un ciclo.

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.

Sintassi

for ( init_expression ; cond_expression ; iteration_expression ) loop_statement

Spiegazione

La sintassi di cui sopra produce codice equivalente a:

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

}

Il init_expression viene eseguito prima dell'esecuzione del ciclo. Il cond_expression valuta al valore, trasformabile in bool. Viene valutata prima di ogni iterazione del ciclo. Il ciclo continua solo se il suo valore è true. Il loop_statement viene eseguito ad ogni iterazione, dopo di che viene eseguito 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.

Parole chiave

for

Esempio

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

Output:

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