The if-esle-if statement
The if...else...if statement:
Here, a user can decide among multiple options. The if statements are executed from the top down. As soon as one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the ladder is bypassed. If none of the conditions is true, then the final else statement will be executed.
Syntax of if...else...if statement is:
if (condition)
statement;
else if (condition)
statement;
.
.
else
statement;
Flowchart of if...else...if statement is show in figure:
Example of if...else...if statement:
#include<iostream>
using namespace std;
int main()
{
int i = 20;
if (i == 10)
cout<<"i is 10";
else if (i == 15)
cout<<"i is 15";
else if (i == 20)
cout<<"i is 20";
else
cout<<"i is not present";
}
Output:
Comments
Post a Comment