Lecture 4 - Decision Controls - IF Statement
Lecture 4 - Decision Controls - IF Statement
if ( yr_of_servc > 3 ) {
bonus = 2500 ;
printf ( "Bonus = Tsh. %d", bonus ) ;
}
}
The If-Else statement
• 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.
• This is what is the purpose of the else
statement will be demonstrated in the
following example:
If-else syntax
If (condition) {
block statement
}
else{
Block statement
}
}
Syntax Rules
1) The If () block is executed when the condition is TRUE
2) The ELSE () block is executed when the condition is
FALSE
3) Else can not exist without IF block
4) If block can exist without Else block
5) Else block can not have a condition
6) If can only pick a single condition
7) When If and Else blocks have just one statement, the
{} becomes an option
Relational operators
• In creating any if block condition, we need
operators
i. < less than
ii. > greater than
iii. <= less than or equal to
iv. >= Greater than or equal to
v. == comparison operator
vi. != not equal to
example
• In a company an employee is paid as under: If
his basic salary is less than tsh. 1500, then
House Rent Allowance (HRA) = 10% of basic
salary and Entertainment Allowance (EA) =
90% of basic salary. If his salary is either equal
to or above tsh. 1500, then HRA = tsh. 500 and
EA = 98% of basic salary. If the employee's
salary is input through the keyboard write a
program to find his gross salary.
/* Calculation of gross salary */
main( ) {
float basic_sal, gross_sal, ent_alw, hse_alw ;
printf ( "Enter basic salary " ) ;
scanf ( "%f", & basic_sal) ;
if (basic_sal < 1500 ) {
hse_alw = basic_sal * 10 / 100 ;
ent_alw = basic_sal * 90 / 100 ;
}
Else {
hse_alw = 500 ;
ent_alw = basic_sal * 98 / 100 ;
}
gross_sal = basic_sal + hse_alw + ent_alw;
printf ( "gross salary = Tsh. %f", gross_sal) ; }
A few points worth noting...
• The group of statements after the if upto and not
including the else is called an ‘if block’.
• Similarly, the statements after the else form the
‘else block’
• Notice that the else is written exactly below the if
• As with the if statement, the default scope of else is
also the statement immediately after the else
Nested if-elses
• 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.
Syntax of if-else for multiple
conditions
If (condition){
block statement }
else if (condition){
block statement }
else if (condition(){
block statement }
else if (condition){
block statement }
Else{
block statement }
}
/* A quick demo of nested if-else */
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 !" ) ;
}
}
r e
c t u
e le
t h
o f
d
En