8.3. do-while – sikl operatori Key Point.do-while – sikl operatori ham while – sikl operatoriga o`xshaydi, farqli tomoni, u birinchi sikl tanasini hisoblaydi, keyin shartni tekshiradi. do-while – sikl operatorining tuzilishi while – sikl operatorining tuzilishidan farq qiladi. Uning snintaksisi quyidagicha:
do {
// Sikl tanasi;
Ko`rsatmalar(s);
} while (siklni-davom-ettirish-sharti);
Uning bajarilish blok-sxemasi 8.2-rasmda keltirilgan.
8.2-rasm. do-while – sikl operatori birinchi sikl tanasini hisoblaydi, keyin hisoblashni davom ettirish yoki to`xtatish uchun shartli ifodani tekshiradi. do-while – sikl operatori birinchi sikl tanasini hisoblaydi. Keyin esa siklni-davom-ettirish-sharti tekshiriladi. Agar shartning qiymati true bo`lsa, sikl tanasi yana hisoblanadi; aks holda, sikl ishi to`xtatiladi.
1 #include
2 using namespace std;
3
4 int main()
5 {
6 // Kiritilgan qiymat 0 bo`lguncha kiritishni saqlash
7 int sum = 0;
8 int data = 0;
9
10 do
11 {
12 sum += data;
13
14 // Keyingi data ni o`qish
15 cout << "Butun qiymat kiriting (kiritish to`xtatiladi" <<
16 "agar u 0 bo`lsa): ";
17 cin >> data;
18 }
19 while (data != 0);
20
21 cout << "Yig`indi = " << sum << endl;
22
23 return 0;
24 }
Butun qiymat kiriting (kiritish to`xtatiladi agar u 0 bo`lsa): 5
Butun qiymat kiriting (kiritish to`xtatiladi agar u 0 bo`lsa): 6
Butun qiymat kiriting (kiritish to`xtatiladi agar u 0 bo`lsa): 0
Yig`indi = 11