If Else If Statement - Control Structures

How If Else If conditional branching Statement is used in C++?

Explanation

The if else if statement is an extension of the "if else" conditional branching statement. When the expression in the "if" condition is "false" another "if else" construct is used to execute a set statements based on a expression. This belongs to Control Structures.

Syntax:


if(expression)
{ Statements }
else if(expression)
{ Statements }
else if(expression)
{ Statements }

Example :



#include <iostream.h>
using namespace std;
int main(void)
{
int per;
cout << "Enter your Percentage::";
cin >> per;
if (per >=80 )
{ cout << "Passed with Distinction" << endl;}
else if (per >=70 )
{ cout << "Passed with High Firstclass" << endl;}
else if (per >=60 )
{ cout << "First Class" << endl; }
else if (per >=50)
{ cout << "Second Class" << endl; }
else
{ cout << "Third Class" << endl; }
return 0;
}
}

Result :

Enter your Percentage::60
First Class

In the above example the if else if statement is used to check multiple conditions based on the percentage of marks got as input. This is the conditional branching function in C++.

C++ Tutorial


Ask Questions

Ask Question