Shartli o’tish operatorlari C++ kursi uchun ma'ruza


Download 1.22 Mb.
Sana26.01.2023
Hajmi1.22 Mb.
#1124514
Bog'liq
if-else

Shartli o’tish operatorlari

C++ kursi uchun ma'ruza

Tanlov konstruksiyalari

C ++ quyidagi shartli iboralarga ega:

    • if Agar belgilangan shart rost bo'lsa, bajarilishi kerak bo'lgan kod blokini belgilash uchun foydalaning
    • Else Agar bir xil shart noto'g'ri bo'lsa, bajarilishi kerak bo'lgan kod blokini belgilash uchun foydalaning
    • else if Agar birinchi shart noto'g'ri bo'lsa, sinov qilish uchun yangi shartni belgilash uchun foydalaning
    • Switch Amalga oshiriladigan ko'plab alternativ kod bloklarini belgilash uchun foydalaning

IF (Agar)

  • If sintaksisi
  • E'tibor bering, if kichik harflar bilan. Katta harflar (If yoki IF) xato keltirib chiqaradi.

if ()
{ // Faqat to'g'ri bo'lgandagi holatni yozasiz
}

if bayonoti

Mantiqiy 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.

  • Operatorlar:
  •  
  • Misol uchun,
  • if ((x > 0) && (a == b)) …


Arifmetik

taqqoslash

Mantiqiy

+ -

< kichik > katta

|| yoki

* / %

== teng
!= teng emas

&& va

Arifmetik funksiyalar

<= kichik yoki teng
>= katta yoki teng

! emas

Operatorning ustuvorligi


Operator

Associativity

Type

( )

L  R

parenthesis

++ – – + – !

R  L

unary operators

* ? %

L  R

multiplicative

+ –

L  R

additive

<< >>

L  R

insertion

< <= > >=

L  R

relational

= = !=

L  R

equality

&&

L  R

and

||

L  R

or

?:

R  L

conditional

= += –= *= /= %=

R  L

assignment

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 << “Passed”;

    else

    cout << “Failed”;

If / else - shart operatorining qisqa ko’rinishi

C++ shartli operatori ternar operatori ->?:

Sintaksis:

o'zgaruvchi= (shart) ? shart to'g'ri bo'lsa shart notug'ri bo'lsa;

  • Misol:
  • grade>=60? cout << “Passed” : cout << “Failed”;

  • yoki:
  • 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 here

if (A > B)

max = A;

else

max = 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';

else

cout <<"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:

  • 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

.

Switch

Ushbu 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