logo

C++ voor lus

De C++ for-lus wordt gebruikt om een ​​deel van het programma meerdere keren te herhalen. Als het aantal iteraties vastligt, wordt aanbevolen om for-lussen te gebruiken in plaats van while- of do-while-lussen.

De C++ for-lus is hetzelfde als C/C#. We kunnen de variabele initialiseren, de voorwaarde controleren en de waarde verhogen/verlagen.

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

Stroomdiagram:

Cpp Voor lus 1

C++ Voor lusvoorbeeld

 #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++ Genest voor lus

In C++ kunnen we een for-lus in een andere for-lus gebruiken, dit staat bekend als geneste for-lus. De binnenste lus wordt volledig uitgevoerd als de buitenste lus één keer wordt uitgevoerd. Dus als de buitenste lus en de binnenste lus vier keer worden uitgevoerd, wordt de binnenste lus vier keer uitgevoerd voor elke buitenste lus, d.w.z. in totaal 16 keer.


C++ Genest voor lusvoorbeeld

Laten we een eenvoudig voorbeeld bekijken van een geneste for-lus in 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++ Oneindig voor lus

Als we een dubbele puntkomma in de for-lus gebruiken, wordt deze oneindig vaak uitgevoerd. Laten we een eenvoudig voorbeeld bekijken van een oneindige for-lus in 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>