int x = 20;
int y = 18;
if (x > y) {
cout << "x is greater than y";
}
Yuqoridagi misolda x ning y dan katta ekanligini tekshirish uchun ikkita o’zgaruvchidan foydalanamiz, x va y> ( operator yordamida). X 20, y 18 va biz 20 ning 18 dan katta ekanligini bilamiz, biz ekranga “x y dan katta” deb chop etamiz.
int time = 20;
if (time < 18) {
cout << "Good day.";
} else {
cout << "Good evening.";
}
// Outputs "Good evening."
Yuqoridagi misolda vaqt (20) 18 dan katta, shuning uchun shart false. Shu sababli, biz shartga o’tamiz elseva “Xayrli oqshom” ekraniga chop etamiz. Agar vaqt 18 dan kam bo’lsa, dastur “Xayrli kun” ni chop etardi.
int time = 22;
if (time < 10) {
cout << "Good morning.";
} else if (time < 20) {
cout << "Good day.";
} else {
cout << "Good evening.";
}
Yuqoridagi misolda vaqt (22) 10 dan katta, shuning uchun birinchi shartfalse . Izohdagi keyingi shart else ifham false, shuning uchun biz shartga o’tamiz, else chunki 1-shart va 2-shart ikkalasi false- va ekranga “Xayrli oqshom” ni chop qilamiz.
Biroq, agar vaqt 14 bo’lsa, bizning dasturimiz “Xayrli kun” ni chop etardi.
Source link
lol