Contact Us

Login Form

If-Else

The if statement is the most simplest Decision making Control statement . It is used to execute a set of one or more Instructions if a specific Condition is True .

Syntax :-

if ( Condition )

{

Statement-Block ;

}

In this

if -----> is a Keyword ( Reserve word )

Condition -----> The Statement-block is executed if Condition is True

(ie. giving Non-Zero result)

We can use any type of valid expression in place of Condition . it can be numeric , constant , variable or any type of complex expression which can give True or False result .

In above , If the condition is true(ie. Non-Zero result) so only the Statement-Block is executed . Otherwise if the condition is False ,the statement-block is Ignored .

I want that you must remember that

The condition must be placed in pair of Bracket , immidiately after the Keyword " if " .

If there is only one single statement in Statement-block so you can avoide/Ignore to use Braces BUT if there are multiple statements in Statement-block , you must put them in Pair of Braces otherwise you will not get your desired Output .

Till this point you know that " if "statement is use to execute a set of instructions if the condition is true But in normal programs we also need to execute some other instructions if the condition is False . For this we use " if else " statement .

if-else statement:-

In " if " statement we execute a set of instruction if the condition is true But it do nothing if the condition is False . As we know in Practical life we also need to execute some instructions if the condition is False . For this purpose , we use " if-else " statement .

Syntax :-

if( Condition )

{

statement-block-1

}

else

{

statement-block-2

}

In this

if -----> is a Keyword

else -----> is a Keyword

In this , if the condition is true (ie. giving Non-Zero result) , it execute the statement-block-1 Otherwise if the condition is False it execute statement-block-2 .

You must be carefull about one thing , if there is one single instruction in statement-block so we can avoid Braces But if there are multiple instructions in statement-block so we must use pair of Braces otherwise it can cause Error signals and also can give unexpected output .The main purpose of using Braces is that according to syntax the keyword " else " must be placed immidiately after if 's statement-block and if we not put if's statement-block in braces it cause error signal .

if ( Condition ) if ( Condition )

{ statement 1 ;

statement 1 ; statement 2 ;

statement 2 ; else

} statement 3 ;

else statement 4 ;

{

statement 3 ; ERROR :- Misplace else

statement 4 ;

}

CORRECT

And also if we not use Braces for "else" it Not give ERROR signal But the Output can be different from our expectation .

eg. if ( Condition ) if ( Condition )

{ {

statement 1 ; statement 1 ;

statement 2 ; statement 2 ;

} }

else else

{ statement 3 ;

statement 3 ; statement 4 ;

statement 4 ;

} This will not give any ERROR signal .

CORRECT But the output comes different from expected

Because Compiler only treat "statement 3" as

else-statement-block and the "statement 4" is

executed every time either condition is True or

False .

This Problem/Mistake is Dangling else Problem .

One more thing , if else can also be used in Nested form ie. we can place several "if-else" structures in "if / else" statement-block .

syntax :-

if( expression-1 )

{

if( expression-2)

{

statement-block-1

}

else

{

statement-block-2

}

}

else

{

if( expression-3)

{

statement-block-3

}

else

{

statement-block-4

}

}

Now we continue to if-else-if Ladder .

if-else-if ladder :-

In above , you can see that we can use if-else statement in Nested form But some-time the nesting of else become very much Complex which is difficult to handle . I clear my point with an example .

Suppose you want to write a program in which the Student Enter the Percentage and the program prints the Division according to the following Range.

Percentage Greater or Equal to 60% 1st division

Percentage Greater from 45% and less from 60% 2nd division

Percentage Greater from 30% and less from 45% 3rd division

Percentage less from 30% NO Division

Now if we try to write it with Nested if-else

the logic for the Percentage is

if( per >=60 )

printf(" 1st Division " ) ;

else

{

if( per >= 45 )

printf(" 2nd Division ") ;

else

{

if( per >= 30 )

printf(" 3rd Division ") ;

else

printf(" NO Division ") ;

}

}

In above example , the chances of mistake are more . Due to this we use if-else-if ladder .

Let's try to write same logic with if-else-if ladder .

if(per >= 60)

printf(" You got 1st Division");

else if( per >= 45)

printf(" You got 2nd Division");

else if(per>=27)

printf("\n You got 3rd Division");

else

printf("\n NO Division ");

Now in this example you can see that using if-else-if ladder is much easier from using Nested-else . It save time , increase generality , easy to handle and decreases the chances of Error . It also increase execution speed and decreases the execution time .