If Else If Ladder in C++
If else statement Definition :
If Else If ladder statement is a series of conditions checked in order. When a condition is met, the program executes the corresponding block of code and bypasses the rest of the ladder.
Syntax:
if(condition 1){ // statements to be executed if condition 1 is true; }
else if(condition 2){
// statements to be executed if condition 2 is true;
}
else{ // statements to be executed if all conditions are false; }
Working of if else if ladder statement :
- Step 1 : In the first step, condition 1 is checked whether it is true or false.
- Step 2 : If condition 1 is True, if block will be executed for performing the operations.
- Step 3 : If the condition 1 is False , condition 2 of else if block will be checked.
- Step 4 : If condition 2 is True, else if block will be executed for performing the operations.
- Step 5 : If all the conditions are False , else block will be executed for performing the operations.
Implementation of If else statement in C++
Example 1:
#include <iostream> using namespace std; int main() { int i=5; if(i>1){ // checking the condition 1 and take decision cout<<"PrepInsta is one of the best for placement preparation"; } else if(i==1){ // checking the condition 1 and take decision cout<<"Want to get placed? Check PrepInsta Prime."; } else{ cout<<"PrepInsta"; } return 0; }
Output:
PrepInsta is one of the best for placement preparation
In the above example , the value of i initialize as 5 and when the condition of if (i>1) is checked , this is taken as true and block if is executed and “PrepInsta is one of the best for placement preparation” will be print on the output screen.
Example 2:
#include<bits/stdcc+.h> using namespace std; int main() { int i=1; if(i>1){ // checking the condition 1 and takes decision cout<<"PrepInsta is one of the best for placement preparation"; } else if(i==1){ // checking the condition 2 and takes decision cout<<"Want to get placed? Check PrepInsta Prime."; else{ cout<<"PrepInsta"; } return 0; }
Output:
Want to get placed? Check PrepInsta Prime.
In the above example , the value of i initialize as 1 and when the condition of if (i>1) is checked , this is taken as false and then else if condition (i==1) is checked which is true and block else if will be executed. Therefore “Want to get placed? Check PrepInsta Prime.” will print on the output screen.
Example 3:
#include<bits/stdc++.h> using namespace std; int main() { int i=0; if(i>1){ // checking the condition 1 and takes decision cout<<"PrepInsta is one of the best for placement preparation"; } else if(i==1){ // checking the condition 2 and takes decision cout<<"Want to get placed? Check PrepInsta Prime."; else{ cout<<"PrepInsta"; } return 0; }
Output:
PrepInsta
In the above example , the value of i initialize as 1 and when the condition of if (i>1) is checked , this is taken as false and then else if condition (i==1) is checked which is false. At last else block will be executed. Therefore “PrepInsta” will print on the output screen.
For more related content : Click-Here
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others