C ++ while și do ... while Loop (cu exemple)

În acest tutorial, vom învăța utilizarea while și do … while bucle în programarea C ++ cu ajutorul unor exemple.

În programarea computerului, buclele sunt folosite pentru a repeta un bloc de cod.

De exemplu, să presupunem că vrem să afișăm un mesaj de 100 de ori. Apoi, în loc să scriem declarația de tipărit de 100 de ori, putem folosi o buclă.

Acesta a fost doar un simplu exemplu; putem obține mult mai multă eficiență și sofisticare în programele noastre prin utilizarea eficientă a buclelor.

Există 3 tipuri de bucle în C ++.

  1. for buclă
  2. while buclă
  3. do… while buclă

În tutorialul anterior, am aflat despre bucla C ++ pentru. Aici vom învăța despre whileși do… whilebucle.

C ++ în timp ce Bucla

Sintaxa whilebuclei este:

 while (condition) ( // body of the loop )

Aici,

  • O whilebuclă evalueazăcondition
  • Dacă conditionevaluează la true, codul din whilebuclă este executat.
  • Este conditionevaluat din nou.
  • Acest proces continuă până când conditioneste false.
  • Când conditionevaluează la false, bucla se termină.

Pentru a afla mai multe despre conditions, vizitați C ++ Operatori relaționali și logici.

Diagrama fluxului while Loop

Diagrama fluxului C ++ în buclă

Exemplul 1: Afișați numerele de la 1 la 5

 // C++ Program to print numbers from 1 to 5 #include using namespace std; int main() ( int i = 1; // while loop from 1 to 5 while (i <= 5) ( cout << i << " "; ++i; ) return 0; )

Ieșire

 1 2 3 4 5

Iată cum funcționează programul.

Repetare Variabil i <= 5 Acțiune
Primul i = 1 true 1 este tipărit și ieste mărit la 2.
Al 2-lea i = 2 true 2 este tipărit și ieste mărit la 3.
A treia i = 3 true 3 este tipărit și ieste mărit la4
Al 4-lea i = 4 true 4 este tipărit și ieste mărit la 5.
Al 5-lea i = 5 true 5 este tipărit și ieste mărit la 6.
Al 6-lea i = 6 false Bucla este terminată

Exemplul 2: Numai suma de numere pozitive

 // program to find the sum of positive numbers // if the user enters a negative number, the loop ends // the negative number entered is not added to the sum #include using namespace std; int main() ( int number; int sum = 0; // take input from the user cout <> number; while (number>= 0) ( // add all positive numbers sum += number; // take input again if the number is positive cout <> number; ) // display the sum cout << "The sum is " << sum << endl; return 0; )

Ieșire

 Introduceți un număr: 6 Introduceți un număr: 12 Introduceți un număr: 7 Introduceți un număr: 0 Introduceți un număr: -2 Suma este 25

În acest program, utilizatorul este solicitat să introducă un număr, care este stocat în numărul variabil.

Pentru a stoca suma numerelor, declarăm o sumă variabilă și o inițializăm la valoarea lui 0.

whileBuclă continuă până când utilizatorul introduce un număr negativ. În timpul fiecărei iterații, numărul introdus de utilizator este adăugat la variabila sumă.

Când utilizatorul introduce un număr negativ, bucla se termină. În cele din urmă, este afișată suma totală.

Faceți C ++ … în timp ce Buclați

do… whileBucla este o variantă a whilebuclei cu o singură diferență importantă: corpul do… whilebuclei este executat o dată înainte conditioneste bifată.

Sintaxa sa este:

 do ( // body of loop; ) while (condition);

Aici,

  • Corpul buclei este executat la început. Apoi conditioneste evaluat.
  • Dacă conditionevaluează la true, corpul buclei din doinstrucțiune este executat din nou.
  • Este conditionevaluat încă o dată.
  • Dacă conditionevaluează la true, corpul buclei din doinstrucțiune este executat din nou.
  • Acest proces continuă până când conditionevaluează false. Apoi bucla se oprește.

Diagrama de a face … în timp ce Bucla

Diagrama C ++ face … while loop

Exemplul 3: Afișați numerele de la 1 la 5

 // C++ Program to print numbers from 1 to 5 #include using namespace std; int main() ( int i = 1; // do… while loop from 1 to 5 do ( cout << i << " "; ++i; ) while (i <= 5); return 0; )

Ieșire

 1 2 3 4 5

Iată cum funcționează programul.

Repetare Variabil i <= 5 Acțiune
i = 1 nu a verificat 1 este tipărit și ieste mărit la 2
Primul i = 2 true 2 este tipărit și ieste mărit la 3
Al 2-lea i = 3 true 3 este tipărit și ieste mărit la 4
A treia i = 4 true 4 este tipărit și ieste mărit la 5
Al 4-lea i = 5 true 5 este tipărit și ieste mărit la 6
Al 5-lea i = 6 false Bucla este terminată

Exemplul 4: Numai suma de numere pozitive

 // program to find the sum of positive numbers // If the user enters a negative number, the loop ends // the negative number entered is not added to the sum #include using namespace std; int main() ( int number = 0; int sum = 0; do ( sum += number; // take input from the user cout <> number; ) while (number>= 0); // display the sum cout << "The sum is " << sum << endl; return 0; )

Ieșire 1

 Introduceți un număr: 6 Introduceți un număr: 12 Introduceți un număr: 7 Introduceți un număr: 0 Introduceți un număr: -2 Suma este 25

Here, the do… while loop continues until the user enters a negative number. When the number is negative, the loop terminates; the negative number is not added to the sum variable.

Output 2

 Enter a number: -6 The sum is 0.

The body of the do… while loop runs only once if the user enters a negative number.

Infinite while loop

If the condition of a loop is always true, the loop runs for infinite times (until the memory is full). For example,

 // infinite while loop while(true) ( // body of the loop )

Here is an example of an infinite do… while loop.

 // infinite do… while loop int count = 1; do ( // body of loop ) while(count == 1);

In the above programs, the condition is always true. Hence, the loop body will run for infinite times.

for vs while loops

A for loop is usually used when the number of iterations is known. For example,

 // This loop is iterated 5 times for (int i = 1; i <=5; ++i) ( // body of the loop )

Here, we know that the for-loop will be executed 5 times.

Cu toate acestea, whileși do… whilebuclele sunt de obicei utilizate atunci când numărul de iterații este necunoscut. De exemplu,

 while (condition) ( // body of the loop )

Consultați aceste exemple pentru a afla mai multe:

  • Program C ++ pentru afișarea seriei Fibonacci
  • Program C ++ pentru a găsi GCD
  • Program C ++ pentru a găsi LCM

Articole interesante...