Sei sulla pagina 1di 19

Control Structure:

Selection
(Part 1)
Knowledge:
Understand various concepts of selection control structure

Skill:
Be able to develop a program containing selection control structure

Computer Science Department

FTSM

Selection Structure

Single
Selection

Multiple
Selection
Double Selection

TK1913-C Programming

Single Selection
Pseudocode Structure
step a
if <condition is true> start
step m
step n
end_if
step x
TK1913-C Programming

Single Selection
Step a
true

condition

Step m

false

Step n

Step x
TK1913-C Programming

Exercise
Develop an algorithm for the following problem:
A system to check/verify the eligibility of a person to vote
during the election is going to be developed. The input for
the system is the persons age.
If the age is greater than 20 years old, then display You
are eligible to vote and Your age is <age> years old
messages.

If the age is less than 20 years old, display Your age is


<age> years old message only.
TK1913-C Programming

Exercise - Answer
start
Assume the age is
12 years old

age
true

12 > 20 ?
FALSE !

age > 20

You are
eligible to vote

false

Your age is 12 yearsYour age is <age>


years old
old
end
TK1913-C Programming

Something to ponder
What if ..
the age is 37 years old?

What about 20 years old?


TK1913-C Programming

Lets recap
Pseudocode Structure

Single Selection

step a
if <condition is true> start
step m
step n
end_if
step x
Double selection has similar
structure except that
TK1913-C Programming

Double Selection
Pseudocode Structure
step a
if <condition is true> start
step m
step n
end_if
else start
step x
step y
end_else
step z
TK1913-C Programming

additional
steps at the
bottom
9

Double Selection
Step a

condition
false

Step x

true

Step m
Step n

Step y
Step z
TK1913-C Programming

10

Exercise
Develop an algorithm for the following problem:
A system to check/verify the eligibility of a person to vote
during the election is going to be developed. The input for
the system is the persons age.
If the age is greater than 20 years old, then display You
are eligible to vote and Your age is <age> years old
messages.

If the age is less than 20 years old, display You are not
eligible to vote and Your age is <age> years old
messages.
TK1913-C Programming

11

Exercise - Answer
start
Verify this
flowchart !

age
true
age > 20

You are
eligible to vote

false
You are not
eligible to vote
Your age is <age>
years old
end
TK1913-C Programming

12

Lets recap
Pseudocode Structure
Pseudocode Structure

Double Selection
Single Selection

step a
step
a
if <condition
is true> start
step m is true> start
if <condition
step n
end_if step m
step n
else start
end_ifstep x
step y
step
x
end_else
Guesshow does multiple
step z

selection look like ?

TK1913-C Programming

13

Multiple Selection
Pseudocode Structure (Multi-way if)
step a
if <condition_1 is true> start
step m
end_if
if <condition_2 is true> start
step n
end_if
step z
TK1913-C Programming

14

Multiple Selection
Step a

Condition1

true

Step m

false
true

Condition2

Step n

false

Step z
TK1913-C Programming

15

Multiple Selection
Pseudocode Structure (Cascaded if)
step a
if <condition_1 is true> start
step m
end_if
if <condition_2 is true> start
step n
end_if
else start
step x
end_else
step z
TK1913-C Programming

16

Multiple Selection
Step a
Condition1

true

Step m

false

true

Condition2

Step n

false

Step x
Step z
TK1913-C Programming

17

Exercise
Develop a flowchart for the following problem.
Given a mark, determine its grade based on the
table below:
74 < mark < 100
64 < mark < 75
54 < mark < 65
39 < mark < 55
0 < mark < 40
others
TK1913-C Programming

grade = A
grade = B
grade = C
grade = D
grade = E
error message
18

End of Lecture 7 (Part 1)


Yes !! Thats all?
Whats next???

PART 2 on the way relax !

TK1913-C Programming

19

Potrebbero piacerti anche