Updated March 20, 2023
Introduction to Else if in C++
As we are already familiar with ‘if and else’ statements, let us now move a bit further into the concept of else if conditions. As the name already suggests that these statements deal with the conditions one after another.
Now let us the concept of utilizing this concept in C++ programming language.
Syntax:
if (first condition)
{
// code to be executed.
}
else if (second condition)
{
//code to be executed
}
else if (third condition)
{
//code to be executed
} …..
else
{
//code to be executed when none of the above conditions is true
}
By the above syntax, we can understand there would be a first if condition and a code to be executed under it. Then we can have multiple conditions, which are to be checked upon. And if none of the conditions work then we are going to execute the code which is written in the else block.
Flow Chart
As per the flow chart, we can note there would be a first if condition. According to the Boolean expression we can have, ‘else if’ condition is to be executed or the code inside the true condition is executed and the program compilation comes out of this if-else condition loop.
Else-If in C++
The condition in C++ is written in below format:
If(condition1)
{
Cout<<"code 1";
}
Else if(condition 2)
{
Cout<<"Code 2";
}
Else if(condition 3)
{
Cout<<"Code 3";
}
……….
Else
{
Cout<<"code to be executed if none of the above conditions is true";
}
‘Else if’ condition is the same for all the programming languages. Here in C++ we only have the syntax level changes to the code but as per the logic, there would be no much change.
Let us have a few examples below to understand working with ‘else if’ statement conditions in C++.
Examples of Else If in C++
The examples are mentioned below:
Example #1
Let’s have a simple example below:
Code:
#include <iostream>
using namespace std;
int main()
{
int a;
cout<<"Enter any number between 1 to 50: ";
cin>>a;
if(a >=0 && a<=10)
{
cout <<" Number chosen is between 0 and 10 ";
}
else if(a >10 && a<=20)
{
cout <<" Number chosen is between 10 and 20 ";
}
else if(a >20 && a<=30)
{
cout <<" Number chosen is between 20 and 30 ";
}
else if(a >30 && a<=40)
{
cout <<" Number chosen is between 30 and 40 ";
}
else if(a >40 && a<=50)
{
cout <<" Number chosen is between 40 and 50 ";
}
else
{
cout<<"The number you chose is out of given range";
}
}
Output:
Another output for that would be:
Example #2
Let us see another example of these conditions:
Code:
#include <iostream>
using namespace std;
int main()
{
int a;
cout<<"Enter any number: ";
cin>>a;
if(a%2 == 0)
{
cout <<" Number chosen is divisible by 2 ";
}
else if(a%3 == 0)
{
cout <<" Number chosen is divisible by 3 ";
}
else if(a%7 == 0)
{
cout <<" Number chosen is divisible by 7";
}
else if(a%11 == 0)
{
cout <<" Number chosen is divisible by 11 ";
}
else if(a%13 == 0 )
{
cout <<" Number chosen divisible by 13 ";
}
else if(a%17 ==0)
{
cout<<" Number chosen is divisible by 17 ";
}
else
{
cout<<"The number chosen is not divisible by 2, 3, 7, 11, 13 and 17";
}
}
Output:
Now, what if I input a number that is divisible by both 2 and 3?
As explained in the flow chart also, if the starting condition gets matched then the code inside that condition is executed and the compiler comes out of that if-else if loop. So, that is why once the divisible condition of 2 is done, the compiler comes out of the ‘if’ conditions and displays is the required output.
Just displaying below the output for the else condition too:
Example #3
Let us have another small example:
Code:
#include <iostream>
using namespace std;
int main()
{
int a,x;
x=1;
cout<<"Original x value is: "<<x<<endl;
cout<<"Enter any number between 1 and 5: ";
cin>>a;
if(a==1)
{
cout <<" This is the main if condition " <<endl;
x=x+1;
cout<< " Value of x is increased by 1 : "<<x;
}
else if(a==2)
{
cout <<" This is second else if condition "<<endl;
x=x+2;
cout<< " Value of x is increased by 2 : "<<x;
}
else if(a==3)
{
cout <<" This is third else if condition " <<endl;
x=x+3;
cout<< " Value of x is increased by 3 : "<<x;
}
else if(a==4)
{
cout <<" This is fourth else if condition "<<endl;
x=x+4;
cout<< " Value of x is increased by 4 : "<<x;
}
else if(a==5 )
{
cout <<" This is fifth else if condition "<<endl;
x=x+5;
cout<< " Value of x is increased by 5 : "<<x;
}
else
{
cout<<"The number choosen is not in between 1 to 5"<<endl;
x=0;
cout<< " Value of x is made to zero : "<<x;
}
}
Output:
One more example can be followed by:
So, few examples for ‘else-if’ statements using C++ are mentioned above.
Conclusion
As we already know, these if conditions form a major part for the logical part of any programming language. The next step is by using ‘else-if’ statements to give us various conditions in handling our program. In an above-mentioned manner, we can have the usage of the conditions for ‘else-if’ through the C++ programming language.
Recommended Articles
This is a guide to Else if in C++. Here we discuss the examples to understand the working of ‘else if’ statement conditions in C++ along with the flowchart. You may also have a look at the following articles to learn more –