[go: up one dir, main page]

0% found this document useful (0 votes)
18 views23 pages

05 Selection

Uploaded by

syed Mahdi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views23 pages

05 Selection

Uploaded by

syed Mahdi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

5: Selection (Decision Making)

Week 4 – ENGG1410

Selection and Decision Making in C


Conditions and Logical Expressions

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 look at the


even have
to
you don't
this e.g)
other pair (m
.

Salary<10000 && Age>=35


– Evaluate false
the expression for the following pair (Salary, Age)
True
True True False False True True
• (9999, 25) (8000, 45) (10500, 20) (12000, 35)
Evaluates to : -
O (false) 1 (true) o (false) 1 (True) :

Recall False is zero, True is non-zero 4


5: Selection (Decision Making)

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

⑧ determine policy type

③ calculate monthly payment (premium /12) +5


↳ for monthly fees .
5: Selection (Decision Making)
Example
float driver_age, premium, month_payment;
int car_doors;
printf(“Enter your age and the number of doors in your car”);

-
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)

What happens if you omit the braces { }


if((car_doors >= 4) && (driver_age > 24) )
premium = 650.00;
printf(“Low Risk\n”);
else
premium = 1200.00;
printf(“High Risk\n”);
month_payment = premiuim /12 + 5;

COMPILE ERROR OCCURS. The “if clause” is the single


statement following the if.

11
5: Selection (Decision Making)

What happens if you omit the braces { }


BRACES CAN ONLY BE OMITTED WHEN EACH CLAUSE IS A
SINGLE STATEMENT
Example:
A phonebook comes in two volumes,
the first volume is for lastnames starting with letter ‘A’
till ‘L’, second volume is from ‘M’ to ‘Z’.

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)

What Is The Output? and Why?

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

• This variable may be an int or a char but NOT a


float or a double
Switch statement
.

b Break is need only


In a

16
5: Selection (Decision Making)

Syntax of switch statement


switch (controlling expression)
{
case set 1: statements;
break;
. . .
. . .
case set n: statements n;
break;
. . .
. . .
default : default statements;
}

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
-

variable for operation .

>
- I

• Write this program once using if-else and the other using switch
statement.

• Modify the program so that If the operation character entered is not


any of the four (+,*,-,/) print an error message to the screen

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)

Example: Mail Order


• Write a program to calculate the total bill price of a certain
purchase. There is a discount and shipping cost:

• The discount rate is 25% and the shipping is $10.00 if purchase is


over $100.00.

• The discount rate is 15% and the shipping is $5.00 pounds


otherwise.

• Sample Output
• For a $120 Purchase your total_bill is $100.
• For a $80 Purchase your total_bill is $73.

22

You might also like