DEV Community

aliyevakumush77
aliyevakumush77

Posted on

if-else and else-if

if-else

  • bir nechta shartlar berilganda ishlatiladi.
#include <iostream>

using namespace std;

int main()
{  
  int baho;
  cin >> baho;
  if (baho >= 90)
  {
    cout << "A" << endl;
  }
  else
  {
    if (baho > 80)
    {
    cout << "B" << endl;
    }
    else
    {
      if (baho > 70)
      {
      cout << "C" << endl;
      }
      else
      {
        if (baho > 60)
        {
        cout << "D" << endl;
        }
        else
        {
        cout << "F" << endl;
        }
      }
    }
  }

  return 0 ;
}
Enter fullscreen mode Exit fullscreen mode
  • agar if yoki else dan so'ng amalga oshirilishi kerak bo'lgan buyruq bitta bo'lsa {} qo'yish shart emas.
#include <iostream>

using namespace std;

int main()
{  
  int baho;
  cin >> baho;

  if (baho >= 90)
  cout << "A" << endl;
  else
    if (baho > 80)
    cout << "B" << endl;
    else
      if (baho > 70)
      cout << "C" << endl;
      else
        if (baho > 60)
        cout << "D" << endl;
        else
        cout << "F" << endl;

  return 0 ;
}
Enter fullscreen mode Exit fullscreen mode

else-if

  • readability ni oshirish uchun else dan so'ng keladigan if ni bir qatorda yozsa else-if hosil bo'ladi.
#include <iostream>

using namespace std;

int main()
{  
  int baho;
  cin >> baho;

  if (baho >= 90)
  cout << "A" << endl;
  else if (baho > 80)
  cout << "B" << endl;
  else if (baho > 70)
  cout << "C" << endl;
  else if (baho > 60)
  cout << "D" << endl;
  else
  cout << "F" << endl;

  return 0 ;
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)