El bucle C++ for s'utilitza per repetir una part del programa diverses vegades. Si el nombre d'iteracions és fix, es recomana utilitzar bucles for que while o do-while.
El bucle C++ for és el mateix que C/C#. Podem inicialitzar la variable, comprovar la condició i augmentar/disminuir el valor.
for(initialization; condition; incr/decr){ //code to be executed }
Diagrama de flux:
Exemple de bucle per a 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'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<=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's see a simple example of infinite for loop in C++.</p> <pre> #include using namespace std; int main () { for (; ;) { cout<<'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++ nidat per bucle
En C++, podem utilitzar el bucle for dins d'un altre bucle for, que es coneix com a bucle for imbricat. El bucle interior s'executa completament quan el bucle exterior s'executa una vegada. Així, si el bucle exterior i el bucle intern s'executen 4 vegades, el bucle intern s'executarà 4 vegades per a cada bucle exterior, és a dir, un total de 16 vegades.
Exemple de bucle for anidat de C++
Vegem un exemple senzill de bucle for imbricat en C++.
#include using namespace std; int main () { for(int i=1;i<=3;i++){ for(int j="1;j<=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's see a simple example of infinite for loop in C++.</p> <pre> #include using namespace std; int main () { for (; ;) { cout<<\'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++ Infinite For Loop
Si utilitzem el doble punt i coma al bucle for, s'executarà infinites vegades. Vegem un exemple senzill de bucle for infinit en C++.
#include using namespace std; int main () { for (; ;) { cout<<\'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>\'infinitive>=3;i++){>=10;i++){>