logo

C++ para loop

O loop for C++ é usado para iterar uma parte do programa várias vezes. Se o número de iterações for fixo, é recomendado usar o loop for em vez dos loops while ou do-while.

O loop for C++ é igual ao C/C#. Podemos inicializar a variável, verificar a condição e aumentar/decrementar o valor.

 for(initialization; condition; incr/decr){ //code to be executed } 

Fluxograma:

Cpp para loop 1

Exemplo de loop para C++

 #include using namespace std; int main() { for(int i=1;i<=10;i++){ cout< <i <<'
'; } < pre> <p>Output:</p> <pre> 1 2 3 4 5 6 7 8 9 10 </pre> <hr> <h2>C++ Nested For Loop</h2> <p>In C++, we can use for loop inside another for loop, it is known as nested for loop. The inner loop is executed fully when outer loop is executed one time. So if outer loop and inner loop are executed 4 times, inner loop will be executed 4 times for each outer loop i.e. total 16 times.</p> <hr> <h2>C++ Nested For Loop Example</h2> <p>Let&apos;s see a simple example of nested for loop in C++.</p> <pre> #include using namespace std; int main () { for(int i=1;i<=3;i++){ for(int j="1;j&lt;=3;j++){" cout< <i<<' '<<j<<'
'; } < pre> <p>Output:</p> <pre> 1 1 1 2 1 3 2 1 2 2 2 3 3 1 3 2 3 3 </pre> <hr> <h2>C++ Infinite For Loop</h2> <p>If we use double semicolon in for loop, it will be executed infinite times. Let&apos;s see a simple example of infinite for loop in C++.</p> <pre> #include using namespace std; int main () { for (; ;) { cout&lt;<'infinitive for loop'; } < pre> <p>Output:</p> <pre> Infinitive For Loop Infinitive For Loop Infinitive For Loop Infinitive For Loop Infinitive For Loop ctrl+c </pre></'infinitive></pre></=3;i++){></pre></=10;i++){>

C++ aninhado para loop

Em C++, podemos usar o loop for dentro de outro loop for, é conhecido como loop for aninhado. O loop interno é executado totalmente quando o loop externo é executado uma vez. Portanto, se o loop externo e o loop interno forem executados 4 vezes, o loop interno será executado 4 vezes para cada loop externo, ou seja, um total de 16 vezes.


Exemplo de loop For aninhado em C++

Vamos ver um exemplo simples de loop for aninhado em C++.

 #include using namespace std; int main () { for(int i=1;i<=3;i++){ for(int j="1;j&lt;=3;j++){" cout< <i<<\' \'<<j<<\'
\'; } < pre> <p>Output:</p> <pre> 1 1 1 2 1 3 2 1 2 2 2 3 3 1 3 2 3 3 </pre> <hr> <h2>C++ Infinite For Loop</h2> <p>If we use double semicolon in for loop, it will be executed infinite times. Let&apos;s see a simple example of infinite for loop in C++.</p> <pre> #include using namespace std; int main () { for (; ;) { cout&lt;<\'infinitive for loop\'; } < pre> <p>Output:</p> <pre> Infinitive For Loop Infinitive For Loop Infinitive For Loop Infinitive For Loop Infinitive For Loop ctrl+c </pre></\'infinitive></pre></=3;i++){>

C++ Infinito For Loop

Se usarmos ponto e vírgula duplo no loop for, ele será executado infinitas vezes. Vamos ver um exemplo simples de loop for infinito em C++.

 #include using namespace std; int main () { for (; ;) { cout&lt;<\'infinitive for loop\'; } < pre> <p>Output:</p> <pre> Infinitive For Loop Infinitive For Loop Infinitive For Loop Infinitive For Loop Infinitive For Loop ctrl+c </pre></\'infinitive>