The if statement by itself will execute a single statement, or a group of statements, when the expression following if evaluates to true. It does nothing when the expression evaluates to false.To execute one group of statements if the expression evaluates to true and another group of statements if the expression evaluates to false we use the else statement .
Example:
In a company an employee is paid as under:
salary and DA = 90% of basic salary. If his salary is either equal to or above Rs. 1500, then HRA = Rs. 500 and DA = 98% of basic salary. If the employee's salary is input through the keyboard write a program to find his gross salary.
main( )
{
float bs, gs, da, hra ;
printf ( "Enter basic salary " ) ;
scanf ( "%f", &bs ) ;
if ( bs <>
{
hra = bs * 10 / 100 ;
da = bs * 90 / 100 ;
}
else
{
hra = 500 ;
da = bs * 98 / 100 ;
}
gs = bs + hra + da ;
printf ( "gross salary = Rs. %f", gs ) ;
}
The flow of control is given as shown below.
Here are the few points to concentrate further.
1 . The group of statements after the if up to and not including the else is called an ‘if block’. Similarly, the statements after the else form the ‘else block’.
2 . Notice that the else is written exactly below the if. The statements in the if block and those in the else block have been indented to the right. This formatting convention is followed throughout the book to enable you to understand the working of the program better.
3 . Had there been only one statement to be executed in the if block and only one statement in the else block we could have dropped the pair of braces.
4 . As with the if statement, the default scope of else is also the statement immediately after the else. To override this default scope a pair of braces as shown in the above example must be used.
78.2
The previous post of the blog deals with Multiple Statements with in IF .
Related Post :
Instructions to C Program
Compilation and Execution of C Program
Rules of writing a C program
You can browse complete 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.

Example:
In a company an employee is paid as under:
salary and DA = 90% of basic salary. If his salary is either equal to or above Rs. 1500, then HRA = Rs. 500 and DA = 98% of basic salary. If the employee's salary is input through the keyboard write a program to find his gross salary.
main( )
{
float bs, gs, da, hra ;
printf ( "Enter basic salary " ) ;
scanf ( "%f", &bs ) ;
if ( bs <>
hra = bs * 10 / 100 ;
da = bs * 90 / 100 ;
}
else
{
hra = 500 ;
da = bs * 98 / 100 ;
}
gs = bs + hra + da ;
printf ( "gross salary = Rs. %f", gs ) ;
}
The flow of control is given as shown below.
Here are the few points to concentrate further.1 . The group of statements after the if up to and not including the else is called an ‘if block’. Similarly, the statements after the else form the ‘else block’.
2 . Notice that the else is written exactly below the if. The statements in the if block and those in the else block have been indented to the right. This formatting convention is followed throughout the book to enable you to understand the working of the program better.
3 . Had there been only one statement to be executed in the if block and only one statement in the else block we could have dropped the pair of braces.
4 . As with the if statement, the default scope of else is also the statement immediately after the else. To override this default scope a pair of braces as shown in the above example must be used.
78.2
The previous post of the blog deals with Multiple Statements with in IF .
Related Post :
Instructions to C Program
Compilation and Execution of C Program
Rules of writing a C program
You can browse complete 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.

0 comments
Post a Comment