Selection statements | Difference between if else and else if



Choice statements



if assertion if~else assertion

  • ikki yoki undan ko’p execution pathlarni tanlaydi
if (a>=0)
cout << "optimistic";
if (a<0)
cout << "unfavourable";
Enter fullscreen mode

Exit fullscreen mode



If assertion

  • if expression is true, execute statement1
if(expression)
{(physique) 
statement1
} 
Enter fullscreen mode

Exit fullscreen mode

if (a>=0)
cout << "optimistic";
Enter fullscreen mode

Exit fullscreen mode

  • do not use semi-colon (;) after if(expression)

    • Agar semi-colon ishlatilsa, if assertion mustaqil bo’lib qoladi.
  • Agar if assertionda faqat bitta assertion bo’lsa {} qo’yilmaydi.
    int a,b,c;

  cin >> a >> b >> c;

  int max = a > b ? a : b;

    if (max > c)
      cout << max << endl;
Enter fullscreen mode

Exit fullscreen mode

  • Agar if assertion situation bajarilmasa output da hech narsa chiqmaydi.
  • If assertion faqat o’zidan keyingi birinchi turgan cout ga ta’sir qiladi.

  • Use == (Realtion operator), do not use = (Task)

    if (staff == '10')
    cout << "Lions" << endl;
not
    if (staff = '10')
        // consequence staff is 10
Enter fullscreen mode

Exit fullscreen mode



If-else assertion

  • If expression is true, execute statement1. If false, execute statement2
  • Syntax
if(expression)
{(physique) 
statement1
} 
else
{(physique) 
statement2
}  
Enter fullscreen mode

Exit fullscreen mode

    if (sonpercent2 == 0)
    cout << "even" << endl;
    else 
    cout << "odd" << endl;
Enter fullscreen mode

Exit fullscreen mode

  • if assertion comprises one other if assertion (together with if-else assertion)
if (a>=0)
   if (xpercent2 ==0)
   cout << "optimistic even quantity" << endl;
   else 
   cout << "optimistic odd quantity" << endl;
Enter fullscreen mode

Exit fullscreen mode



else-if assertion

    if (a>='A' && a<='Z')
    cout << "Higher-case" << endl;
        else if (a>='a' && a<= 'z')
        cout << "Decrease-case" << endl;
            else if (a>='0' && a<='9')
            cout << "Quantity" << endl;
                else 
                cout << "Others" << endl;
Enter fullscreen mode

Exit fullscreen mode

Add a Comment

Your email address will not be published. Required fields are marked *