DEV Community

Mohira21
Mohira21

Posted on

6 2

Selection statements | Difference between if else and else if

Selection statements

if statement if~else statement

  • ikki yoki undan ko'p execution pathlarni tanlaydi
if (a>=0)
cout << "positive";
if (a<0)
cout << "negative";
Enter fullscreen mode Exit fullscreen mode

If statement

  • if expression is true, execute statement1
    • Syntax
if(expression)
{(body) 
statement1
} 
Enter fullscreen mode Exit fullscreen mode
  • Example
if (a>=0)
cout << "positive";
Enter fullscreen mode Exit fullscreen mode
  • don't use semi-colon (;) after if(expression)
    • Agar semi-colon ishlatilsa, if statement mustaqil bo'lib qoladi.
  • Agar if statementda faqat bitta statement 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 statement condition bajarilmasa output da hech narsa chiqmaydi.
  • If statement faqat o'zidan keyingi birinchi turgan cout ga ta'sir qiladi.

  • Use == (Realtion operator), don't use = (Assignment)

    if (team == '10')
    cout << "Lions" << endl;
not
    if (team = '10')
        // result team is 10
Enter fullscreen mode Exit fullscreen mode

If-else statement

  • If expression is true, execute statement1. If false, execute statement2
  • Syntax
if(expression)
{(body) 
statement1
} 
else
{(body) 
statement2
}  
Enter fullscreen mode Exit fullscreen mode
  • Example
    if (son%2 == 0)
    cout << "even" << endl;
    else 
    cout << "odd" << endl;
Enter fullscreen mode Exit fullscreen mode
  • if statement contains another if statement (including if-else statement)
if (a>=0)
   if (x%2 ==0)
   cout << "positive even number" << endl;
   else 
   cout << "positive odd number" << endl;
Enter fullscreen mode Exit fullscreen mode

else-if statement

  • Example
    if (a>='A' && a<='Z')
    cout << "Upper-case" << endl;
        else if (a>='a' && a<= 'z')
        cout << "Lower-case" << endl;
            else if (a>='0' && a<='9')
            cout << "Number" << endl;
                else 
                cout << "Others" << endl;
Enter fullscreen mode Exit fullscreen mode

Do your career a big favor. Join DEV. (The website you're on right now)

It takes one minute, it's free, and is worth it for your career.

Get started

Community matters

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay