The words you are searching are inside this book. To get more targeted content, please make full-text search by clicking here.
Discover the best professional documents and content resources in AnyFlip Document Base.
Search
Published by inaazlin, 2021-02-06 10:49:50

2.1 Design a solution Part 2

2.1 Design a solution Part 2

2.1(c ) Explain Each control structure and
their respective purposes

Control Structures

• The structure of a program which decides the order of
program statement executions is known as control
structure

• The control structure evaluates statements according
to the results

• Thus, control structure affects the flow of a code

Godse, 2008

Control Structures

There are three types of control structures:
i. Sequence
ii. Selection
iii. Repetition/ Looping

Sequence Control Structures

• Instructions are written in such way that they are executed
sequentially one after another from top to bottom.

• Instructions simply follow one another in a logical
progression

PROBLEM : NON COMPUTING SITUATION

Write a PSEUDOCODE and draw a FLOW
CHART to show the right way to wash your
hands.

PROBLEM : NON COMPUTING SITUATION

PSEUDOCODE

START
Wet your hands with clean water.
Lather your hands with soap.
Scrub your hands for at least 20 seconds.
Rinse your hands with clean water.
Dry your hands using a clean towel.

STOP

PROBLEM : NON COMPUTING SITUATION

FLOW CHART START

Wet your hands with clean water.

Lather your hands with soap.
Scrub your hands for at least 20 seconds.

Rinse your hands with clean water.
Dry your hands using a clean towel.

STOP

Sequence Control Structures

Example 1:

Find the sum and average of three numbers input by
user.

Sequence Control Structures

Problem Analysis:
Input : num1, num2, num3
Process : Calculate sum and average of three numbers
Output : sum, average

Sequence Control Structures

Pseudocode
START

READ num1, num2, num3
sum = num1+num2+num3
average=(num1+num2+num3)/3
DISPLAY sum, average
STOP

Sequence Control Structures

Flowchart START

Input num1, num2, num3

sum = num1+num2+num3

average = (num1+num2+num3)/3

Print sum, average

STOP

Example 2

Finds the angle of a triangle if two angles
are given.

Step 1 : Identify IPO
Step 2 : Transfer IPO to pseudocode / flowchart

Example 2 : Identify IPO (STEP1)

Finds the angle of a triangle if two angles
are given.

Input angle1, angle2
Process
Output Calculate the angle3 of a
triangle

angle3

Example 2 : Transfer IPO to pseudocode / flowchart

Pseudocode Flowchart

START

START READ angle1,
READ angle1, angle2 angle2

angle3 = 180 – (angle1 + angle2) angle3 = 180 – (angle1 +
PRINT angle3 angle2)

STOP

PRINT angle3

STOP

Control Structure : Selection

Use of selection structure in daily life:
i. Choosing between a hot or cold

drink.
ii. Making a right or left turn at a

junction.
iii. Selecting which scarf to wear to

class.
iv. Deciding whether or not to iron a

shirt.
v. Picking side dishes at Kenny Roger

Roaster’s Restaurant.

15

Control Structure : Selection

• Also known as decision structure.
• Purpose:

Makes a decision (based on some condition) and then takes an
appropriate action based on that decision.

16

Selection Control Structures

• A selection control structure checks the specified
condition for which the answer may be yes or no (i.e true
or false)

• The sequence of execution of the further actions is decided
according to this answer

Control Structure : Selection

Example 1: condition
if (it is raining) action

bring an umbrella

Example 2: condition
if (you have a quiz tomorrow) action

study tonight condition
otherwise action

watch a movie

Types of Selection Control Structure

• Single Selection Single selection (if-selection)
• Dual Selection
• Multiple Selection Follow the set of instructions only when the condition is true

Dual selection (if-else selection)

Follow different set of instructions for true or false conditions

Multiple selection (nested if-else / nested if )

Follow the set of instructions based on the condition chosen

Selection Control Structure -Single
Selection (if)

if structure
➢Checks a condition to perform action(s).
➢If condition is true, perform action(s)
➢If condition is false, skips it (i.e. perform nothing)

Selection Control Structure -Single
Selection (if)

Pseudocode Format Flowchart Format

if <condition>
<action>

Selection Control Structure -Single
Selection (if)

Example 1:

Create a program that will accept student’s mark. If the mark
is greater than 40, print the message “PASS”.

Selection Control Structure -Single
Selection (if)

Problem Analysis

Input : mark
Process : Determine message based on mark
Output : “PASS” or No Output

Selection Control Structure -Single
Selection (if)

Pseudocode

START

READ mark
if mark greater than 40

PRINT “PASS”
endif

STOP

Selection Control Structure -Single
Selection (if)

Flowchart START

READ mark false

mark greater than 40

true
PRINT“PASS”

STOP

Selection Control Structure -Single Selection (if)

Create a program that will accept student’s mark. If the mark is greater than 40, print

the message “PASS”.

START

Input Mark READ mark
Process
Determine message mark greater than 40
output based on mark
true
“Pass”or No output PRINT“PASS”

false

Start STOP
read mark
if mark > 40
print “ Pass”
end if

Stop"

Selection Control Structure –
Dual Selection (if-else)

if-else structure
checks one condition to choose between two actions
If condition is true, perform action1
If condition is false, perform action2

Dual Selection
If else conditions

• contains two set of instructions. Follow different set of
instructions for true or false conditions.

Example: Instruction Set 1
if (you have a quiz tomorrow) Processed when
condition is true
study tonight
else Instruction Set 2
Processed when
watch a movie condition is false
end if

If- Else SDeuleacl tSioelnection

2 • Follow dIifffeerlesnetcsoent doiftiionnstsructions for
true or false conditions

if (you are a female)
use the toilet on your left

else
use the toilet on your right

end if

Selection Control Structure –
Dual Selection (if-else)

Pseudocode Format Flowchart Format

if <condition>
<action1>

else

<action2>

Selection Control Structure –
Dual Selection (if-else)

Example 1:
Create a program that will accept student’s mark. If a
student’s mark is greater than 40, print the message
“PASS”. Otherwise, print the message “FAIL”.

Selection Control Structure –
Dual Selection (if-else)

• Problem Analysis

Input : mark
Process : Determine message based on mark
Output : “PASS” or “FAIL”

Selection Control Structure –
Dual Selection (if-else)

• Pseudocode

START
READ mark
if mark greater than 40
PRINT “PASS”
else
PRINT “FAIL”
endif

STOP

Selection Control Structure –
Dual Selection (if-else)

• Flowchart START

READ mark false
PRINT“FAIL”
mark greater than 40

true
PRINT “PASS”

STOP

Example 2 : if-else selection

Problem Statement:
If x is greater than y, display “x is bigger than y” else display “x is
smaller than y”.
input X,Y
Process Determine message based on input.
Output Message “x is bigger than y” or “x is smaller
than y”.

Start Start
read x,y Read x,y
if x greater than y
print “x is bigger than y” T
else
print “x is smaller than y” x>y
end if
F Print “x is
Stop bigger than y”
Print “x is
smaller than

y”

Stop

Selection Control Structure –
Multiple Selection (if-else-if)

if-else-if structure
• Checks many conditions to choose between many actions
• Condition will be checked one by one
• When a condition is true, perform the action and stop
checking the rest.

Multiple selection – (nested if-else/ nested if)

3 Follow the set of instructions based on the
condition chosen

if (you are going to Banggol Judah)
take the right turn

else
if (you are going to Kg. Joh)
take the left turn
else
go straight

end if

Multiple selection – (nested if-else/ nested if)

3 Follow the set of instructions based on the
condition chosen

if (you are going to Banggol Judah)
take the right turn

else
if (you are going to Kg. Joh)
take the left turn
else
go straight
end if

end if

Selection Control Structure –
Multiple Selection (if-else-if)

• Pseudocode format

if <condition1>
<action1>

elseif <condition2>
<action2>
…..

else
<last action>

Selection Control Structure –
Multiple Selection (if-else-if)

• Flowchart format

condition1 true action1

false true

condition2 action2

false


condition n true

false action n

Last action

Selection Control Structure –
Multiple Selection (if-else-if)

Example 1:

Create a program to accept a number from user, and to determine

whether the number is positive, negative or zero.

Input number
Process
Determine whether number is positive, negative or
output zero

Message “ positive” or “ negative “ or zero

Selection Control Structure –
Multiple Selection (if-else-if)

Pseudocode

START
READ number
if number greater than 0
PRINT “positive”
else if number less than 0
PRINT “negative”
else
PRINT “zero”
end if
end if

STOP

Selection Control Structure –
Multiple Selection (if-else-if)

• Flowchart START

READ number true

number greater than 0 PRINT “positive”

false true

number less than 0 PRINT “negative”

false
PRINT “zero”

STOP

Example 2 : multiple selection if-else

• Calculate the average of 3 tests for a
student. Determine and display the grade
based on the grading table below.

Average Grade
Greater than or equal to 80 A
Greater than or equal to 60 B
Greater than or equal to 50 C
Below than 50 Fail

45

Example 2 : (IPO analysis)

Input: Test1, Test2, Test3
Process: Calculate the average and

determine the grade based on the average.

Output: “A” or “B” or “C” or “Fail”

input test1, test2, test3
Process
1.Calculate average based on test1,test2, test3
Output 2. Determine grade based on average

grade

Example 2 : Pseudocode

Start
Enter Test1, Test2, Test3
average = (Test1 + Test2 + Test3)/3
if (average >= 80)
display “Grade A”
else
if (average >= 60)
display “Grade B ”
else
if (average >= 50)
display “Grade C”
else
display “Fail”
end if
end if
end if

Stop

Example 2 : Pseudocode

Start
enter Test1, Test2, Test3
average = (Test1 + Test2 + Test3)/3
if (average >= 80)
Grade=‘A’
else
if (average >= 60)
Grade=‘B’
else
if (average >= 50)
Grade=‘C’
else
display “Fail”
end if
end if
end if
display Grade

Stop

Example 2 : flowchart

start

Read Test1, Test2, Test3

average=
(Test1+Test2+Test3)/3

average>= 80 T Print
F “Grade A”

average>= 60 T Print
F “Grade B”

average>= 50 T Print
F “Grade C

Display “Fail”

stop

Summary selection

Purpose: Makes a decision (based on some condition) and then takes an
appropriate action based on that decision

SINGLE SELECTION DUAL SELECTION MULTIPLE SELECTION

Check a condition to Check one condition to Check many conditions
perform action choose between two to choose between
If condition true -> action many actions.
perform action If condition true -> Condition will be
If condition false -> perform action1 checked one by one.
skip it ( perform If condition false-> When condition is true
nothing) perform action2 performs the action
and stops checking the
rest.


Click to View FlipBook Version