DEV Community

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

Posted on

C++ Nested if...else

Assalamu aleykum xurmatli dasturchi bugun siz bilan C++ dasturlash tilida Nested if - ichma ich if ni kirib chiqamiz.

Ba'zan biz boshqa if ichida yani bitta if ** iborasidan foydalanishimiz kerak. Bu ichki o'rnatilgan **if iborasi sifatida tanilgan.

Buni if iboralarining bir nechta qatlami sifatida tasavvur qiling. Birinchi, tashqi if gap, uning ichida esa boshqa, ichki if gap bor.
Uning sintaksisi:

// outer if statement
if (condition1) {

  // statements

  // inner if statement
  if (condition2) {
    // statements
  }
}
Enter fullscreen mode Exit fullscreen mode

Eslatmalar:

  • Agar kerak bo'lsa, ichki if iborasiga else va else if iboralarini qo'shishimiz mumkin.

  • Ichki if iborasi tashqi else yoki else if iboralari (agar mavjud bo'lsa) ichiga ham kiritilishi mumkin.

  • Biz if iboralarining bir nechta qatlamlarini joylashtirishimiz mumkin.

Misol uchun:

#include <iostream>
#include <cmath>
#include <iosmanip>
using namespace std;

int main()
{

  int son;
  cin >> son;

  if(son > 0)
  {
    if(son % 2 == 0)
    {
      cout << "Musbat juft son";
    }
    else
    {
      cout << "Musbat toq son";
    }
  }
  else
  {
    if(son % 2 == 0)
    {
      cout << "Manfiy juft son";
    }
    else
    {
      cout << "Manfiy toq son";
    }
  }


  return 0;
}
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
jurabek777 profile image
jurabek777

perfect explanation