Shartli o’tish operatorlari C++ kursi uchun ma'ruza
Download 1.22 Mb.
|
if-else
Shartli o’tish operatorlariC++ kursi uchun ma'ruzaTanlov konstruksiyalariC ++ quyidagi shartli iboralarga ega:
IF (Agar)
if ( { // Faqat to'g'ri bo'lgandagi holatni yozasiz } if bayonotiMantiqiy ifoda to'g'ri yoki noto'g'ri bo'lgan shartni ifodalaydi.U operandlar (doimiylar, o'zgaruvchilar) va operatorlar (arifmetik operatorlar, relyatsion operatorlar, mantiqiy operatorlar) yordamida tuziladi.
if ((x > 0) && (a == b)) …
Operatorning ustuvorligi
If / else
if (grade >= 60)cout << “Passed”;elsecout << “Failed”;If / else - shart operatorining qisqa ko’rinishiC++ shartli operatori ternar operatori ->?:Sintaksis:o'zgaruvchi= (shart) ? shart to'g'ri bo'lsa: shart notug'ri bo'lsa;
grade>=60? cout << “Passed” : cout << “Failed”;cout << (grade >=60? “Passed” : “Failed”);3 ta sondan kattasini topuvchi dastur// Finding the largest of 3 values#include int main(){int A, B, C, max;cout << "Enter 3 values, and I will tell you which is largest.\n";cin >> A >> B >> C; //really should have user prompts hereif (A > B)max = A;elsemax = B;if (C > max)max = C;cout << "The maximum is " << max << endl;return 0;}Ichma ich joylashgan if// Ushbu dastur test ballini harf bahosiga aylantiradi.#include int main(){int score;cout << "Enter the test score: "; cin >> score;if (score > 100) cout << "Error: score is out of range." ;else if (score >= 90) cout << 'A';else if (score >= 80) cout << 'B';else if (score >= 70) cout << 'C';else if (score >= 60) cout << 'D';else if (score >= 0) cout << 'F';elsecout <<"Error: score is out of range.";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:
. SwitchUshbu dastur ham test ballini harf bahosiga aylantiradi.#include 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 5: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; }Download 1.22 Mb. Do'stlaringiz bilan baham: |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling