Selection Control Structures: Problem Concept
Selection Control Structures: Problem Concept
Selection Control
Structures
Problem Concept
Learning Outcomes
Learning Outcomes
Apply selection control structure (simple
selection, multiple selection, and nested selection)
algorithms
Apply case construct in pseudocode
Recognize algorithms using variations of
function.
in
Introduction
Introduction
Define the following:
Decision Making
Subject
One or more Outcomes
Selection Control
Structure
IF-ELSE Selection
Switch Case Selection
Selection Control
Structure
You can use the selection control structure in
Keywords
(6 Basic Computer
Operations)
Condition
IF Im Rich THEN
Outcome(s)
Go to Starbucks
ELSE
Go to roadside mamak stalls
ENDIF
10
IF status == Y THEN
Display Yes!!
ELSE
Display No!!
ENDIF
12
Wait wait
Whats the difference between = and == ?
=
Assignment Operator
==
Comparison Operator
13
14
5 IF-ELSE Selections
1.
2.
3.
4.
5.
Simple Selection
Simple Selection (with null ELSE)
Combine Selection (AND, OR)
NOT Selection
Nested Selection
Linear (IF followed by ELSE)
Non-linear (Mixed e.g. IF followed by another
IF)
15
16
simple IF structure
It is used when a task is performed only when
17
20
21
22
Linear Nested IF
IF marks >= 80 THEN
Display Grade is A
ELSE
IF marks >= 75 THEN
Display Grade is A-
ELSE
IF marks >= 70 THEN
Display Grade is B+
ENDIF
ENDIF
ENDIF
Simple Selection
Simple Selection
Simple Selection
with null ELSE
23
Non-Linear Nested IF
IF marks >= 40 THEN
IF marks >= 75 THEN
IF marks >= 80 THEN
Display Grade is A
ELSE
Display Grade is A-
ENDIF
ELSE
Display Grade is B+ or less
ENDIF
ENDIF
Simple Selection
Simple Selection
Simple Selection
with null ELSE
24
Example
An electric power distribution company charges its
200
401 600 RM2.30 plus RM 0.80 per unit excess 0f
400
601 and above
RM3.90 plus RM 1.00 per unit
excess 0f 600
Limitations
Variable limited to int and char
Value checked valid for singled-value
instead of ranged-value.
27
How do we write?
CASEOF number
1: Display Number 1
2: Display Number 2
other: Display Other than 1 and 2
ENDCASE
29
30
Exercises
31
Extra Question 1
Kuala Lumpur sometimes has very hazy conditions,
and raise health concern among the residence. A
pollutant hazard index has been developed. If the
index rises above 100, the air is listed as
unhealthful. If the index rises above 200, a
first-stage haze alert is issued and certain
activities are restricted. If an index goes over 275,
a second-stage alert is called and more severe
restrictions apply. Write a program that takes an
input the daily hazard index and identifies the
alert situation.
32
Extra Question 2
Write a program that reads an employees
number, hours worked, and hourly rate and
calculates his or her wages. All hours over 40
are paid at 1.5 times the regular hourly rate.
33
Extra Question 3
Design a solution print a letter grade given
and examination score.
Grades and marks
A 4-point grading system is adopted:
Grade
Marks Range Grade Point
Description
A
80 -100 4.00
Excellent
A75 -79
3.70
Excellent
B+
70 -74
3.30
Very Good
B
65 -69
3.00
Good
B60 -64
2.70
Good
C+
55 -59
2.30
Satisfactory
C
50 -54
2.00
Pass
D
40 -49
1.00
Passing
F
0 -39
0.00
Fail
Extra Question 4
Design a solution to calculate the water bill given the cubic
meter of water used for PUAS Water Company, which charges
the homeowner one of the following:
A. A flat rate of RM10.00 for usage up to and including 100
cubic meter.
B. $0.20 per cubic meter for usage over 100 cubic meter and
up to and including 200 cubic meter.
C. $0.35 per cubic meter for usage over 200 cubic meter
Extra Question 5
A customer needs a specific amount of paper. The
charges on the paper are RM0.10 for single sheets,
RM0.07 for amounts in multiples of 100 sheets,
RM0.05 in multiples of 500 sheets, and RM0.03 in
multiples of 1000 sheets.
Develop a solution to calculate the type and
number of package(s) for the least amount of
money the customer should buy. given the
minimum amount of sheets the customer needs.
For example, if the customer needs 380 sheets, the amount
she would pay when buying in multiples of 100 would be 36
Extra Question 6
A hotel has a pricing policy as follows:
a.
b.
c.
d.
2 people: RM85
3 people: RM90
4 people: RM95
Additional people: RM25 per person
Function
Self - defined function
Defining function
A function have 2 main components
1.Function header(function declaration)
2.Function body (functions program codes)
(arguments)
Function definition
1.Function header
return data type
Function definition
2.Function body
The body contain all the program codes that
Function definition
(example)
int
{
int total = 0;
total = number1 + number2;
return (total);
}
In the above example, it is a simple function named add.
The add function will receive 2 integer type data
(arguments) from the program, it will add them up and
pass the integer data total (return data type is int)
back to the program which called the function using the
return statement.
Accessing a function
A function can be accessed (called) by specifying
Accessing a function
(example)
In the previous example, we created an add
Program Structure
main
(void)
Local Declaration
Statements
main
(void)
Local Declaration
Statements
}
Self-defined function
Function prototypes
Function prototypes are line of codes use to inform the
function name
(argument );
Function prototypes
(example)
The function prototype looks similar to the
Program Structure
(example)
#include <stdio.h>
int add (int number1, int number2); // function prototype
int main (void)
{
int total = 0; number1 = 0; number2 = 0;
printf(\nPlease enter first number \n>);
scanf(%d,&number1);
printf(\nPlease enter second number \n>);
scanf(%d,&number2);
Summary
56
Summary
This chapter covered the selection control
structure in detail
Descriptions and pseudocode examples were