Multiple Statements within if in C

It may so happen that in a program we want more than one statement to be executed if the expression following if is satisfied. If such multiple statements are to be executed then they must be placed within a pair of braces as illustrated in the below example.

Example:

The current year and the year in which the employee joined the organization are entered through the keyboard. If the number of years for which the employee has served the organization is greater than 3 then a bonus of Rs. 2500/- is given to the employee. If the years of service are not greater than 3, then the program should do nothing.

/Calculation of Bonus */

main()
{

int bonus, cy, yoj, yr_of_ser;

printf(“Enter current year and year of joining”);
scanf(“%d %d”,&cy, &joj);

yr_of_ser=cy-joj;

if(yr_of_ser>3)
{
bonus=2500;
printf(“Bonus =Rs. %d”,bonus);
}

}

In the above program we observe that

  • The two statements to be executed on satisfaction of the condition have been enclosed within a pair of braces.
  • If a pair of braces is not used then the C compiler assumes that the programmer wants only the immediately next statement after the if to be executed on satisfaction of the condition.
  • In other words we can say that the default scope of the if statement is the immediately next statement after it.
multiple if statement
multiple  statement under if

 

Satya Prakash

VOIP Expert: More than 8 years of experience in Asterisk Development and Call Center operation Management. Unique Combination of Skill Set as IT, Analytics and operation management.

Leave a Reply