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

 

Related Posts

  • 81
    Decision !!! we all need to alter our actions in the face of changing circumstances. If the weather is fine, then I will go for a stroll. If the highway is busy I would take a diversion. If the pitch takes spin, we would win the match. If she says…
    Tags: executed, statement, decision, control, structure, program, default, programming, write, introduction
  • 76
    Like most languages, C uses the keyword if to implement the decision control instruction. The general form of if statement looks like this: if ( this condition is true )       execute this statement ; The keyword if tells the compiler that what follows is a decision control…
    Tags: statement, condition, number, program, example, executed, control, decision, learn, programming
  • 61
    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. Can we execute one group of statements if the expression evaluates to true and another group of statements…
    Tags: statement, statements, rs, expression, program, write, learn, example, programming, tutorial
  • 60
    The programs that we have learned/developed in this tutorial so far used either a sequential or a decision control instruction. In the first one, the calculations were carried out in a fixed order, while in the second, an appropriate set of instructions were executed depending upon the outcome of the condition…
    Tags: statement, control, executed, program, condition, decision, number, structure, tutorial, programming
  • 55
    It is perfectly all right if we 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. Let's have an example: /* A quick demo of nested if-else */ main( ) { int i ;…
    Tags: statement, program, condition, write, learn, tutorial, programming

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

Your email address will not be published. Required fields are marked *

%d bloggers like this: