There are following types of conditional statements in C++ :
- if statement
- if - else statement
- nested if-else statement
- if -else if ladder
- switch statement
4. If-else if ladder - The statements inside the body of “if” only execute if the given condition returns true, if not then the "else if"condition is checked , then if the true is returned then "else if" condition is executed and if the condition returns false then the statements inside “else” is executed.
Syntax :
if (condition)
{
// Body of the loop.
// These statements will only execute if the condition is true.
}
else if(condition)
{
// Body of the loop.
// These statements will only execute if the condition is true.
}
else
{
// Body of the loop.
// These statements will only execute if all the above conditions are false.
}
Flow Diagram of If-else if ladder
Example of If-else if ladder in C++
#include<iostream.h>
#include<conio.h>
int main()
{
int a=15,b=10;
if(a<b) //if condition statement
{
cout<<"Value of a is less than b"; //display output
}
else if(a>b) //else if condition statement
{
cout<<"Value of b is less than a."; //display output
}
else //else condition statement
{
cout<<"Value of a is equal to b."; //display output
}
getch();
}
OUTPUT:
Value of b is less than a.
0 Comments
If you have any doubts , Please let me know.