Tässä opetusohjelmassa opetellaan while ja do… while -silmukoiden käyttö C ++ - ohjelmoinnissa joidenkin esimerkkien avulla.
Tietokoneohjelmoinnissa silmukoita käytetään toistamaan koodilohko.
Oletetaan esimerkiksi, että haluamme näyttää viestin 100 kertaa. Sitten voimme käyttää silmukkaa sen sijaan, että kirjoittaisimme tulostuslausekkeen 100 kertaa.
Se oli vain yksinkertainen esimerkki; voimme saavuttaa paljon enemmän tehokkuutta ja hienostuneisuutta ohjelmissamme käyttämällä silmukoita tehokkaasti.
C ++: ssa on 3 tyyppistä silmukkaa.
for
silmukkawhile
silmukkado… while
silmukka
Edellisessä opetusohjelmassa opimme C ++ -silmukasta. Täällä aiomme oppia while
ja do… while
silmukoita.
C ++ silmukan aikana
while
Silmukan syntaksi on:
while (condition) ( // body of the loop )
Tässä,
while
Silmukka arvioicondition
- Jos
condition
arvioidaantrue
,while
suoritetaan silmukan sisällä oleva koodi . condition
Arvioidaan uudelleen.- Tämä prosessi jatkuu, kunnes
condition
onfalse
. - Kun
condition
arviointi onfalse
, silmukka päättyy.
Lisätietoja conditions
C ++ Relational and Logical Operators -sivustosta.
Vuokaavio silmukasta

Esimerkki 1: Näytä numerot 1-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; )
Tuotos
1 2 3 4 5
Näin ohjelma toimii.
Toisto | Vaihteleva | i <= 5 | Toiminta |
---|---|---|---|
1 | i = 1 | true | 1 on painettu ja i korotettu arvoon 2 . |
2. | i = 2 | true | 2 on tulostettu ja i korotettu arvoon 3 . |
3. | i = 3 | true | 3 on painettu ja i korotettu arvoon4 |
4. päivä | i = 4 | true | 4 on painettu ja i korotettu arvoon 5 . |
5. | i = 5 | true | 5 on painettu ja i korotettu arvoon 6 . |
6. | i = 6 | false | Silmukka on päättynyt |
Esimerkki 2: Vain positiivisten lukujen summa
// 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; )
Tuotos
Syötä numero: 6 Syötä numero: 12 Syötä numero: 7 Syötä numero: 0 Syötä numero: -2 Summa on 25
Tässä ohjelmassa käyttäjää kehotetaan syöttämään numero, joka on tallennettu muuttujan numeroon.
Numeroiden summan tallentamiseksi ilmoitamme muuttuvan summan ja alustamme sen arvoon 0
.
while
Silmukka jatkuu, kunnes käyttäjä syöttää negatiivinen luku. Kunkin iteraation aikana käyttäjän syöttämä numero lisätään summa-muuttujaan.
Kun käyttäjä syöttää negatiivisen luvun, silmukka päättyy. Lopuksi näytetään kokonaissumma.
C ++ tehdä … silmukan aikana
do… while
Silmukka on muunnelma while
silmukan yhdellä tärkeällä erolla: ruumiin do… while
silmukka suoritetaan kerran ennen condition
tarkistetaan.
Sen syntaksi on:
do ( // body of loop; ) while (condition);
Tässä,
- Silmukan runko suoritetaan aluksi. Sitten
condition
arvioidaan. - Jos arvo
condition
ontrue
,do
lausekkeen sisällä olevan silmukan runko suoritetaan uudelleen. condition
Arvioidaan uudelleen.- Jos arvo
condition
ontrue
,do
lausekkeen sisällä olevan silmukan runko suoritetaan uudelleen. - Tämä prosessi jatkuu, kunnes
condition
arvioidaanfalse
. Sitten silmukka pysähtyy.
Vuokaavio tehtävästä … samalla kun silmukka

Esimerkki 3: Näytä numerot 1-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; )
Tuotos
1 2 3 4 5
Näin ohjelma toimii.
Toisto | Vaihteleva | i <= 5 | Toiminta |
---|---|---|---|
i = 1 | ei tarkistettu | 1 tulostetaan ja i korotetaan arvoon 2 |
|
1 | i = 2 | true | 2 on tulostettu ja i nostettu arvoon 3 |
2. | i = 3 | true | 3 tulostetaan ja i nostetaan 4: een |
3. | i = 4 | true | 4 on tulostettu ja i nostettu arvoon 5 |
4. päivä | i = 5 | true | 5 on tulostettu ja i nostettu arvoon 6 |
5. | i = 6 | false | Silmukka on päättynyt |
Esimerkki 4: Vain positiivisten lukujen summa
// 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; )
Lähtö 1
Syötä numero: 6 Syötä numero: 12 Syötä numero: 7 Syötä numero: 0 Syötä numero: -2 Summa on 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.
Kuitenkin while
ja do… while
silmukoita käytetään tavallisesti, kun määrä toistojen on tuntematon. Esimerkiksi,
while (condition) ( // body of the loop )
Katso nämä esimerkit saadaksesi lisätietoja:
- C ++ -ohjelma Fibonacci-sarjan näyttämiseen
- C ++ -ohjelma GCD: n löytämiseksi
- C ++ -ohjelma LCM: n löytämiseksi