C++ do-while cilpa tiek izmantota, lai vairākas reizes atkārtotu programmas daļu. Ja iterāciju skaits nav fiksēts un cilpa ir jāizpilda vismaz vienu reizi, ieteicams izmantot do-while cilpu.
C++ do-while cilpa tiek izpildīta vismaz vienu reizi, jo stāvoklis tiek pārbaudīts pēc cilpas pamatteksta.
do{ //code to be executed }while(condition);
Blokshēma:
C++ do-while cilpas piemērs
Apskatīsim vienkāršu C++ do-while cilpas piemēru, lai izdrukātu 1. tabulu.
#include using namespace std; int main() { int i = 1; do{ cout< <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'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< <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<<'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++ Nested do-while Loop
Programmā C++, ja izmantojat do-while cilpu citā do-while cilpā, to sauc par ligzdotu do-while cilpu. Ligzdotā do-while cilpa tiek pilnībā izpildīta katrai ārējai do-while cilpai.
Apskatīsim vienkāršu ligzdotas do-while cilpas piemēru C++.
#include using namespace std; int main() { int i = 1; do{ int j = 1; do{ cout< <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<<\'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
C++ valodā, ja nokārtosi taisnība cilpā do-while tā būs infinitīva do-while cilpa.
do{ //code to be executed }while(true);
C++ Infinitīvs do-while Loop piemērs
#include using namespace std; int main() { do{ cout<<\'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>\'infinitive>