DEV Community

Cover image for C++ if ... else statements
islomAli99
islomAli99

Posted on

C++ if ... else statements

Assalamu aleykum xurmatli dasturchi bugun siz bilan C++ dasturlash tilida if ... else statement xaqida korib chiqamiz.

C++ Conditions and If Statements

C++ shartlari va If bayonotlari

Siz allaqachon bilasizki, C++ matematikadan odatiy mantiqiy shartlarni qo'llab-quvvatlaydi:

  • Less than: a < b.
  • Less than or equal to: a <= b
  • Greater than: a > b
  • Greater than or equal to: a >= b
  • Equal to a == b
  • Not Equal to: a != b

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

C++ dasturlash tilida quydagi selection statementlar mavjud:

  • Agar belgilangan shart rost bo'lsa, bajariladigan kod blokini belgilash uchun if dan foydalaning.

  • Agar birinchi shart noto'gri bo'lsa bajariladigan kod blokini belgilash uchun else dan foydalaning.

  • Agar birinchi shart noto'gri bo'lsa, sinov uchun yangi shartni belgilash uchun else if dan foydalaning.

  • Bajarilishi kerak bo'lgan ko'plab muqobil kod bloklarini belgilash uchun kalitdan foydalaning.

The if statement.
if - Agar shart true bo'lsa, bajarilishi kerak bo'lgan C++ kod blokini belgilash uchun if iborasidan foydalaning.

Syntax:

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

E'tibor bering, agar kichik harflar bilan yozilgan. Katta harflarga o'zgartirilsa (Agar yoki IF) xatoga olib keladi.

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

#include <iostream>
using namespace std;

int main()
{

int son = 20;
int son1 = 18;

if(son > son1)
{
cout << "20 is greater than 18";

}


return 0;
}
Enter fullscreen mode Exit fullscreen mode

Biz o'zgaruvchilarni ham sinab ko'rishimiz mumkin:

#include <iostrea>
using namespace std;

int main()
{

int x = 20;
int y = 18;
if (x > y) 
{
  cout << "x is greater than y";
}

return 0;
}
Enter fullscreen mode Exit fullscreen mode

Misol tushuntirildi:

Yuqoridagi misolda x ning y dan katta ekanligini tekshirish uchun ikkita o'zgaruvchidan foydalanamiz, x va y (> operatori yordamida). X 20, y 18 va biz 20 ning 18 dan katta ekanligini bilamiz, biz ekranga “x y dan katta” deb chop etamiz.

C++ Else

The else Statement

Agar shart noto'g'ri bo'lsa, bajariladigan kod blokini belgilash uchun else iborasidan foydalaning.
Syntax:

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

Misol uchun:

#include <iostream>
using namespace std;

int main()
{

int time = 20;
if (time < 18) {
  cout << "Good day.";
} else {
  cout << "Good evening.";
}
// Outputs "Good evening."


return 0;
}
Enter fullscreen mode Exit fullscreen mode

Misol tushuntirildi:
Yuqoridagi misolda vaqt (20) 18 dan katta, shuning uchun shart noto'g'ri. Shu sababli, biz boshqa shartga o'tamiz va ekranga "Xayrli oqshom" ni chop qilamiz. Agar vaqt 18 dan kam bo'lsa, dastur "Xayrli kun" ni chop etardi.

C++ else if
Agar birinchi shart notog'ri bo'lsa yangi shartni belgilash uchun else if iborasidan foydalaning.

if (condition1) {
  // block of code to be executed if condition1 is true
} else if (condition2) {
  // block of code to be executed if the condition1 is false and condition2 is true
} else {
  // block of code to be executed if the condition1 is false and condition2 is false
}
Enter fullscreen mode Exit fullscreen mode

Misol uchun:

#include <iostream>
using namespace std;

int main()
{

int time = 22;
if (time < 10) {
  cout << "Good morning.";
} else if (time < 20) {
  cout << "Good day.";
} else {
  cout << "Good evening.";
}
// Outputs "Good evening."


return 0;
}
Enter fullscreen mode Exit fullscreen mode

Misol tushuntirildi:
Yuqoridagi misolda vaqt (22) 10 dan katta, shuning uchun birinchi shart noto'g'ri. Keyingi shart, else if iborasida ham noto'g'ri, shuning uchun biz boshqa shartga o'tamiz, chunki shart 1 va shart 2 ham noto'g'ri - va ekranga "Xayrli oqshom" ni chop eting.

Biroq, agar vaqt 14 bo'lsa, bizning dasturimiz "Xayrli kun" ni chop etardi.

Top comments (0)