if-else Statement

Default Image

As you observe in previous post
example if we enter a number greater than 50 it shows us a result that
“Number is greater than 50” and if we enter a number smaller than 50 it
does not tell us anything just skip the statement who’s execution
depends upon condition and execute next line that shows a message of
“End of execution”. We have no alternate that tells us our condition
went wrong that’s why you have not any response so, to eliminate such
confusion we have if-else structure in C++.

In if-else structure we have an
opportunity to make two-way decisions. In this structure we use one
condition and two statements or blocks. When the given condition is true
one statement execute and if it is false the other statement will
execute. The general structure of if-else structure is given below:


statement-1;

if(condition)

statement-2;

else

statement-3;


In this structure after execution of
statement-1 if the given condition is true compiler will execute
statement-2 if it is false then statement-3 execute. In case we have
more then one statements that will execute upon fulfillment of condition
the syntax will be as given below:


statement-1;

if(condition)

{

block-1;

}

else

{

block-2;

}


Flow chart of if-else structure is given below:


if-else flow chart






#include

#include

void main()

{

int a;

clrscr();

cout<>a;

if(a>50)

cout<<“Number is greater than 50”;

else

cout<<“Number is smaller than 50”;

getch();

}

Read Count: 3 times

Comments

No comments yet.

Leave a Comment