logo

C++ Do-While Loop

Cyklus do-while v C++ sa používa na opakované opakovanie časti programu. Ak počet iterácií nie je pevný a musíte vykonať cyklus aspoň raz, odporúča sa použiť cyklus do-while.

Cyklus do-while v C++ sa vykoná aspoň raz, pretože po tele cyklu sa kontroluje podmienka.

 do{ //code to be executed }while(condition); 

Vývojový diagram:

Cpp Vykonajte počas cyklu 1

Príklad slučky do-while v C++

Pozrime sa na jednoduchý príklad cyklu C++ do-while na vytlačenie tabuľky 1.

 #include using namespace std; int main() { int i = 1; do{ cout&lt; <i<<'
'; i++; } while (i <="10)" ; pre> <p>Output:</p> <pre> 1 2 3 4 5 6 7 8 9 10 </pre> <hr> <h2>C++ Nested do-while Loop</h2> <p>In C++, if you use do-while loop inside another do-while loop, it is known as nested do-while loop. The nested do-while loop is executed fully for each outer do-while loop.</p> <p>Let&apos;s see a simple example of nested do-while loop in C++.</p> <pre> #include using namespace std; int main() { int i = 1; do{ int j = 1; do{ cout&lt; <i<<'
'; j++; } while (j <="3)" ; i++; (i 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++ Infinitive do-while Loop</h2> <p>In C++, if you pass <strong>true</strong> in the do-while loop, it will be infinitive do-while loop.</p> <pre> do{ //code to be executed }while(true); </pre> <hr> <h2>C++ Infinitive do-while Loop Example</h2> <pre> #include using namespace std; int main() { do{ cout&lt;<'infinitive do-while loop'; } while(true); < pre> <p>Output:</p> <pre> Infinitive do-while Loop Infinitive do-while Loop Infinitive do-while Loop Infinitive do-while Loop Infinitive do-while Loop ctrl+c </pre></'infinitive></pre></i<<'
';></pre></i<<'
';>

C++ vnorená slučka do-while

Ak v C++ použijete slučku do-while v inej slučke do-while, je známa ako vnorená slučka do-while. Vnorená slučka do-while sa úplne vykoná pre každú vonkajšiu slučku do-while.

Pozrime sa na jednoduchý príklad vnorenej slučky do-while v C++.

 #include using namespace std; int main() { int i = 1; do{ int j = 1; do{ cout&lt; <i<<\'
\'; j++; } while (j <="3)" ; i++; (i 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++ Infinitive do-while Loop</h2> <p>In C++, if you pass <strong>true</strong> in the do-while loop, it will be infinitive do-while loop.</p> <pre> do{ //code to be executed }while(true); </pre> <hr> <h2>C++ Infinitive do-while Loop Example</h2> <pre> #include using namespace std; int main() { do{ cout&lt;<\'infinitive do-while loop\'; } while(true); < pre> <p>Output:</p> <pre> Infinitive do-while Loop Infinitive do-while Loop Infinitive do-while Loop Infinitive do-while Loop Infinitive do-while Loop ctrl+c </pre></\'infinitive></pre></i<<\'
\';>

C++ Infinitive do-while Loop

V C++, ak prejdete pravda v slučke do-while to bude infinitívna slučka do-while.

 do{ //code to be executed }while(true); 

Príklad C++ Infinitive do-while

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