0% found this document useful (0 votes)
3 views

Lecture 4 - Decision Controls - IF Statement

Uploaded by

samwelgmtakati
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Lecture 4 - Decision Controls - IF Statement

Uploaded by

samwelgmtakati
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 19

LECTURE 4: DECISION CONTROLS

• Checks on the condition based on condition to


choose direction
• Condition can either be TRUE(1) or FALSE (-)
• It is implemented using the concept of If------Else
• a decision control instruction can be implemented
in C using:
• The if statement
• The if-else statement
• The conditional operators
If statement
• 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 instruction.
– If the condition is not true then the statement is not
executed;
e.g.
• Here is a simple program, which demonstrates
the use of if and the relational operators.
/* Demonstration of if statement */
main( ) {
int num ;
printf ( "Enter a number less than 10: " ) ;
scanf ( "%d", &num ) ;
if ( num <= 10 )
printf ( "What an obedient servant you are !" ) ;
}
Decision control flow chart
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, cyear, yoj, yr_of_servc ;
printf ( "Enter current year and year of joining: " ) ;
scanf ( "%d %d", &cy, &yoj ) ;
yr_of_servc = cyear - yoj ;

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

You might also like