The decision control structure in C can be implemented in C using
(a) The if statement
(b) The if - else statement
(c) The conditional operators
The if Statement
The general form of if statement looks like this:
if (this condition is true)
execute this statement;
Here the keyword if tells the compiler that what follows, is a decision control instruc- tion. The condition following the keyword if is always enclosed within a pair of parentheses. If the condition, whatever it is true, then the statement is executed. It the condition is not true then the statement is not executed instead the program skips past it. The condition in C is evaluated using C’s relational operators. The relational operators help us to build expression, which are either true or false.
(a) The if statement
(b) The if - else statement
(c) The conditional operators
The if Statement
The general form of if statement looks like this:
if (this condition is true)
execute this statement;
Here the keyword if tells the compiler that what follows, is a decision control instruc- tion. The condition following the keyword if is always enclosed within a pair of parentheses. If the condition, whatever it is true, then the statement is executed. It the condition is not true then the statement is not executed instead the program skips past it. The condition in C is evaluated using C’s relational operators. The relational operators help us to build expression, which are either true or false.
Expression | Is true if |
X = = Y | X is equal to Y |
X ! = Y | X is not equal to Y |
X | X is less than Y |
X>Y | X is greater than Y |
X< = Y | X is less than or equal to Y |
X> = Y | X is greater than or equal to Y |
Demonstration of if statement
main( )
{
int num;
printf(“Enter a number less than 10”);
scanf(“%d”, & num);
if(num < = 10)
printf(“The number is less than 10”);
}
Multiple Statements within if If it is desired that more than one statement is to be executed if the condition following if is satisfied, then such statements must be placed within pair of braces. e.q The following program demonstrate that if year of service greater than 3 then a bonus of Rs. 2500 is given to employee.
The program illustrates the multiple statements used within if
/* calculation of bonus */
main( )
{
int bonus, CY, Yoj, yr-of-ser;
printf(“Enter current year and year of joining”);
scanf(“%d %d”, &cy, &yoj);
yr-of-ser = CY-Yoj;
if(yr-of-ser > 3)
{
bonus = 2500;
printf(“Bonus = Rs. %d”, bonus);
}
}
If - else The if statement by itself will execute a single statement or a group of statements when the condition following if is true. it does nothing when the condition is false. It the condition is false then a group of statements can be executed using else statement. The following program illustrates this
/* Calculation of gross salary */
main( )
{
float bs, gs, da, hra;
printf(“Enter basic salary”);
scanf(“%f”, & bs);
if(bs <1500) hra =" bs" da =" bs" hra =" 500;" da =" bs" gs =" bs+hra+da;" salary =" Rs." style="font-weight: bold;">Nested if - else If we write an entire if - else construct within the body of the if statement or the body of an else statement. This is called ‘nesting’ of ifs.
e.g.
if(condition)
{
if (condition)
do this;
else
}
{
do this;
and this;
}
else
do this;