+1 vote
in Class 12 by kratos

Explain Nested if-else-if statement general format with suitable example.

1 Answer

+6 votes
by kratos
 
Best answer

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 *** a set of statements based on the expression.

Syntax:

– if(expression)

{ statements } else if(expression)

{ statements } else if(expression)

{ statements }

include<iostream.h>

int main(void)

{

int per;

cout<<"Enter your percentage::";

cin>>per;

if(per>=80)

{

cout<<"passed with Distinction" <<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. The user entered percentage as 60, then first condition evaluated and returned with false. This resulted in checking for second if condition and resulted in true which give the print “first class”.

...