if-else if Statement

Default Image

nother form of nested
if-else if if-else if statement. It is use of else when there are more
than one conditional statements that may all execute to true, yet you
want only one if statement’s block to execute. You can use an “else if”
structure, if statement and its body; that way, if the first statement
is true, the “else if” will be ignored, but if the if statement is
false, it will then check the condition for the else if statement. If
the if statement was true, the else statement will not be checked. It is
possible to use numerous else if statements to ensure that only one
block of code is executed. Use of last else is optional.

flow chart for is-else if statement


Let’s start with an example:


#include

#include

void main()

{

clrscr();

int a;

cout<<"Enter a value "; cin>>a;

if (a>=15){

cout<<"Number is greater than or equal to 15"; } else if (a>=10){

cout<<"Number is greater than or equal to 10"; } else if (a>=5){

cout<<"Number is greater than or equal to 5";

}

else {

cout<<"Number is smaller than 5";

}

getch();

}


Output of the above programme is given below:


if-else if result 1

if-else if result 2



Source Code for if-else if structure can be downloaded by clicking here.

Read Count: 3 times

Comments

No comments yet.

Leave a Comment