An Interest In:
Web News this Week
- March 21, 2024
- March 20, 2024
- March 19, 2024
- March 18, 2024
- March 17, 2024
- March 16, 2024
- March 15, 2024
May 10, 2022 08:38 am GMT
Original Link: https://dev.to/mohira21/selection-statements-difference-between-if-else-and-else-if-4icd
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";
If statement
- if expression is true, execute statement1
- Syntax
if(expression){(body) statement1}
- Example
if (a>=0)cout << "positive";
- 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;
- 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
If-else statement
- If expression is true, execute statement1. If false, execute statement2
- Syntax
if(expression){(body) statement1} else{(body) statement2}
- Example
if (son%2 == 0) cout << "even" << endl; else cout << "odd" << endl;
- 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;
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;
Original Link: https://dev.to/mohira21/selection-statements-difference-between-if-else-and-else-if-4icd
Share this article:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To