05 Selection
05 Selection
Week 4 – ENGG1410
If – else
Nested If - else
switch
1
5: Selection (Decision Making)
Lecture Outline
• Using Relational and Logical Operators to
Construct and Evaluate Logical Expressions used
in selection statements
• If-Else Statements
• Nested if-else
• Switch statement
2
5: Selection (Decision Making)
CONTROL STRUCTURES
• C control structures mean the control flow of execution in a
program or a function.
• The three control structures to control execution flow are:
① – Sequence ② Selection
=
③ Repetition
• This lecture describes the C control structure for selection.
• The selection control structure is a control structure that
chooses among alternative program statement according to a
condition.
• A condition is an expression that is either false (represented
by 0) or true (usually represented by 1).
3
5: Selection (Decision Making)
CONDITIONS
• Most conditions that we can use are performed using the
equality/relational operators and can be combined or
expanded by logical operators
logical operators
J
When one
↳ Note : - Short circuits
Relational operators- of the expressions is false ;
Example
int age, height, weight;
age = 25; height = 70; weight = 145;
1 true
⑧ false-
Or -
>
-
① true ② false
-
T
I (true)
3 I Chines -
False false
3 o
.
(false)
5
5: Selection (Decision Making)
IF – ELSE STATEMENT
• In C the if statement is the primary selection control
structure.
If the condition /
3
will only run
is true-
expression
6
5: Selection (Decision Making)
Example
• if the student’s grade is greater than or equal to 60 then
print Passed
7
5: Selection (Decision Making)
Example
• Print Passed if the student’s grade is greater than or equal
to 60 and print Failed if the student’s grade is less than 60.
8
5: Selection (Decision Making)
Example
• The car insurance policy is based on two factors, the age of
the driver and the number of doors in the car.
• Policy is considered “low risk” if the driver is above 24
years of age and and the car has 4 doors or more, in which
case the premium is $650 per year. Otherwise, the policy is
“high risk” with a premium of $1200 per year.
• Write a program to determine the monthly payment for
customer based on entered age and number of car doors.
Note there is an extra $5 per month administrative fees.
--
variables I
$5 per
month Fees -
1 doors
doors
age Int If age
2400 :
float 650$·
risk Premium
650 low
=
-
Prem -
eise
& high risk
1200
premium
=
1200$
9
Algo : -
① Reed doors
age and
-
scanf("%d%f\n", &age, &car_doors);
if((car_doors >= 4) && (driver_age > 24) )
and
{
premium = 650.00;
printf(“Low Risk\n”);
}
else
{
premium = 1200.00;
printf(“High Risk\n”);
}
month_payment = premium /12 + 5;
printf(“your monthly payment is $%7.2f”,month_payment);
10
5: Selection (Decision Making)
11
5: Selection (Decision Making)
Al in
if(last_initial <= ‘K’)
·
volume =1;
else
volume =2;
printf(“Look it up in Volume %d of the phonebook”, volume);
12
5: Selection (Decision Making)
int age;
age=30;
if (age < 18)
printf(”Do you Drive?”);
printf(”Too Young to Vote”);
G output will be this
will
He if statement fase the program
why ? as
is
,
to the next
,
statement
skip Statement
the ,
I go
.
which is "Too to drive"
young
how the
& even the It doesn't make sense , that's
13
program works]
·
5: Selection (Decision Making)
Nested If Statements
• If student’s grade is greater than or equal to 90 Print “A”
else
If student’s grade is greater than or equal to 80
Print “B” else
If student’s grade is greater than or equal to 70 Print “C”
else
If student’s grade is greater than or equal to 60
Print “D” else
Print “F”
Oct 3 end 8
-
-
14
5: Selection (Decision Making)
Nested If Statements
Oct 3 end 8
-
-
15
5: Selection (Decision Making)
Switch statement
• USED shen selecting among many alternatives
BASED on a single variable
16
5: Selection (Decision Making)
17
5: Selection (Decision Making)
Example
char color;
printf(“enter a color letter: “;
scanf(“%c”,&color);
switch(color)
{
case ‘R’: printf(“Red\n”); break;
case ‘G’: printf(”Green\n”);break;
case ‘B’: printf(“Blue\n”);break;
default : printf(”Not an RGB color\n”);
}
18
5: Selection (Decision Making)
Example
• Write a program that asks a user to enter two integer values, then
asks him to choose an operation to perform on these numbers by
entering the operation character as follows:
+: addition *: multiplication -: subtraction /: division
Then display the result. > 2 variables for 2 Int values
-
>
- I
• Write this program once using if-else and the other using switch
statement.
19
5: Selection (Decision Making)
#include<stdio.h>
Example If statement
main()
{int x,y,result; char p; version :
printf(“please enter the first number:”);
scanf(“%d”,&x);
printf(“please enter the second number:”);
scanf(“%d”,&y);
printf(“please enter an operation(+ - * /):”);
scanf(“%c”,&p); not used for
if (p==‘+’) single' sign is for assigning ,
- conditional Statement
.
result=x+y;
else if (p==‘-’)
result=x-y;
·
m
elseamun
if (p==‘-’)
result=x-y;
·
else if (p==‘*’)
result=x*y;
else if (p==‘/’)
result=x/y;
printf(“result= %d:”,result);
}
20
5: Selection (Decision Making)
#include<stdio.h>
Example Switch Statement
main() -
version
{int x,y,result; char p;
printf(“please enter the first number:”);
scanf(“%d”,&x);
printf(“please enter the second number:”);
scanf(“%d”,&y);
printf(“please enter an operation(+ - * /):”);
·
scanf(“%c”,&p);
switch(p)
{
case ‘+’: result=x+y; break;
case ‘-’: result=x-y; break;
case ‘*’: result=x*y; break;
case ‘/’: result=x/y; break;
}
-
> Printf is result)
% d ,
;
Return 0; ("Result
}
21
5: Selection (Decision Making)
• Sample Output
• For a $120 Purchase your total_bill is $100.
• For a $80 Purchase your total_bill is $73.
22