We can use to write an entire if-else construct within either the body of the if statement or the body of an else statement. This is called ‘nesting’of ifs.

Note that the second if-else construct is nested in the first else statement. If the condition in the first if statement is false, then the condition in the second if statement is checked. If it is false as well, then the final else statement is executed.

You can see in the program how each time a if-else construct is nested within another if-else construct, it is also indented to add clarity to the program. Inculcate this habit of indentation, otherwise you would end up writing programs which nobody (you included) can understand easily at a later date.

In the sample program an if-else occurs within the else block of the first if statement. Similarly, in some other program an if-else may occur in the if block as well. There is no limit on how deeply the ifs and the elses can be nested.

Here is the sample code to execute the nested if else statements.

main( )

{

int i ;

printf ( "Enter either 1 or 2 " ) ;

scanf ( "%d", &i ) ;

if ( i == 1 )

printf ( "You would go to heaven !" ) ;

else

{

if ( i == 2 )

printf ( "Hell was created with you in mind" ) ;

else

printf ( "How about mother earth !" ) ;

}

}

Related Posts :

IF ELSE statements in C
Multiple statements in IF
IF statements in C Programming
Association of operators in C

Learn entire C Programming Course here .

Thank you for visiting PROGRAMMING BLOG. If you liked the post, please subscribe to my blog via email or RSS FEED.You can contact me here for any specific feed back .


COMMENT HERE and thank you for sparing your valuable time.

I will be very glad if you share this page on your social book marking site with the below link.


Share/Save/Bookmark

0 comments

Post a Comment