DEV Community

faxridd1n
faxridd1n

Posted on

if...else

C++ matematikadan odatiy mantiqiy shartlarni qo'llab-quvvatlaydi:

Kichik: a < b
Kichik yoki teng: a <= b
Katta: a > b
dan katta yoki teng: a >= b
a == b ga teng
Teng emas: a != b

Turli qarorlar uchun turli harakatlarni bajarish uchun ushbu shartlardan foydalanishingiz mumkin.

C++ da quyidagi shartli bayonotlar mavjud:

  1. Agar belgilangan shart rost bo'lsa, bajariladigan kod blokini belgilash uchun if dan foydalaning
  2. Agar bir xil shart noto'g'ri bo'lsa, bajariladigan kod blokini belgilash uchun else dan foydalaning
  3. Agar birinchi shart noto'g'ri bo'lsa, sinov uchun yangi shartni belgilash uchun else if dan foydalaning
  4. Bajariladigan ko'plab muqobil kod bloklarini belgilash uchun kalitdan foydalaning

Agar shart rost bo'lsa, bajarilishi kerak bo'lgan C++ kod blokini belgilash uchun if iborasidan foydalaning.

if (condition) {
  // block of code to be executed if the condition is true
}
Enter fullscreen mode Exit fullscreen mode

Quyidagi misolda 20 ning 18 dan katta ekanligini aniqlash uchun ikkita qiymatni sinab ko'ramiz. Agar shart rost bo'lsa, matnni chop eting:

if (20 > 18) {
  cout << "20 is greater than 18";
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)