8.5-kodli ro`yxat. BarchaQiymatniOlish.cpp 1 #include 2 #include 3 using namespace std;
4
5 int main()
6 {
7 // Faylni ochish
8 ifstream sonlar("score.txt");
9
10 double sum = 0;
11 double son;
12 while (!sonlar.eof()) // Fayl oxiri bo`lmasa davom etish
13 {
14 sonlar >> son; // Ma’lumotni o`qish
15 cout << son << " "; // Ma’lumotni konsolga chiqarish
16 sum += son;
17 }
18
19 sonlar.close();
20
21 cout << "\nYig`indi = " << sum << endl;
22
23 return 0;
24 }
95 56 34
Yig`indi = 185
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