If / else - if/else sintaksisi
- if <mantiqiy ifoda> <amal1> [else <amal2>];
- Misol: Agar talabning bahosi 60 dan yuqoti bo’lsa imtihondan o’tdi aks holda o’tolmadi.
- C++:
- if (grade >= 60)
- cout << “O`tdi”;
- else
- cout << “O`tmadi”;
If / else - shart operatorining qisqa ko’rinishi - C++ shartli operatori ternar operatori ->?:
- Sintaksis:
- o'zgaruvchi= (shart) ? shart to'g'ri bo'lsa: shart noto`g'ri bo'lsa;
- Misol:
- grade>=60? cout << “O`tdi” : cout << “O`tmadi”;
- yoki:
- cout << (grade >=60? “O`tdi” : “O`tmadi”);
- // 3 ta kiritilgan sondan kattasini topish dasturi
- #include
-
- int main(){
- int A, B, C, max;
-
- cout << “3 ta ixtiyoriy sonni kiriting!\n";
- cin >> A >> B >> C;
- if (A > B)
- max = A;
- else
- max = B;
- if (C > max)
- max = C;
- cout << “Sonlarning kattasi: " << max << endl;
- return 0;
- }
- if (Mantiqiy_ifoda)
- Blok 1_ 1
- else
- Blok 1_ 2
Ichma ich joylashgan if - // Ushbu dastur test ballini 5 ballik bahoga aylantiradi.
- #include
- int main(){
- int t;
- cout << “Test balingizni kiriting!: "; cin >> t;
- if (t > 100) cout << “Xatolik! Ball xato kritilgan!" ;
- else if (t >= 90) cout << ‘5';
- else if (t >= 80) cout << ‘4';
- else if (t >= 70) cout << ‘3';
- else if (t >= 60) cout << ‘2';
- else cout <<“Siz imtihondan o`ta olmadingiz!";
-
- return 0;
- }
C++ Switch (Tanlash Operatori) - Sintaksisi:
- switch(qiymat) {
- case x: // code block break;
- case y: // code block break;
- default: // code block }
- Bu quyidagicha ishlaydi:
- switchIfoda bir marta baholanadi
- Ifodaning qiymati har birining qiymatlari bilan taqqoslanadi case
- Agar mos keladigan bo'lsa, tegishli kod bloki bajariladi
- break tanlash operatorini ishini yakunlaydi
- defaultkalit so'zlar ixtiyoriy bo'lgan qiymat uchun ishlaydi. yuqoridagi birontasi mos kelmasi demak default ishlaydi
- CONTROLLING EXECUTION - SELECTION
Switch - Ushbu dastur test ballini harf bahosiga aylantiradi.
-
- #include
- Using namespace std;
- int main(){
- int score;
- cout << "Enter the test score: ";
- cin >> score;
- switch (score/10) {
- case 10:
- case 9: cout << 'A' << endl; break;
- case 8: cout << 'B' << endl; break;
- case 7: cout << 'C' << endl; break;
- case 6: cout << 'D' << endl; break;
- case 4:
- case 3:
- case 2:
- case 1:
- case 0: cout << 'F' << endl; break;
- default: cout << "Error: score is out of range.\n";
- }
- return 0; }
Do'stlaringiz bilan baham: |