0% found this document useful (0 votes)
43 views14 pages

2.2 Control Sturcture

The document discusses the three main control structures used in algorithms: sequence, selection, and repetition. It explains that pseudocode is used to describe algorithms before programming. Pseudocode examples are provided to illustrate each control structure, including sequencing steps, conditional branching with if/else, and repeating loops with while. The last example provides pseudocode to add numbers from 1 to n and check if the sum is greater than 10.

Uploaded by

Mohd Khalil
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)
43 views14 pages

2.2 Control Sturcture

The document discusses the three main control structures used in algorithms: sequence, selection, and repetition. It explains that pseudocode is used to describe algorithms before programming. Pseudocode examples are provided to illustrate each control structure, including sequencing steps, conditional branching with if/else, and repeating loops with while. The last example provides pseudocode to add numbers from 1 to n and check if the sum is greater than 10.

Uploaded by

Mohd Khalil
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/ 14

Problem Solving

Control Structures and Pseudocode


Learning Outcome
• At the end of this session, the student is expected to:
o Acknowledge the three control structures used to design algorithms:
• Sequence Structure
• Selection Structure
• Repetition Structure
o Apply the three control structures in pseudocode to describe an algorithm

Control Structures & Pseudocode


Control Structures
• An algorithm is a step-by-step instructions to solve a problem
• Control structures control the flow of execution in a program or
function.
• In 1966, two researchers, C. Bohm and G. Jacopini, demonstrated
that all program can be written using 3 control structures: sequence,
selection and repetition.

Control Structures & Pseudocode


Pseudocodes
• A pseudocode is an informal language that is used to develop an
algorithm before programming (implementation in SDM).
• It is convenient, user friendly but not an actual programming
language.
• There is no correct one way of writing a pseudocode, as long as it is
clear.
• You may use the word “begin” and “end” to mark beginning and
end of pseudocode
• Criteria of a good pseudocode:
o Easy to understand, precise and clear
o Proper indentations

Control Structures & Pseudocode


Pseudocodes: The sequence control structure
• A series of steps or statements that are executed in the order they are written in
an algorithm.
• Example (printing user’s age):
Begin
Read the year of birth from the user
Calculate difference between year of birth with this year
Print the user age.
End

Control Structures & Pseudocode


Pseudocodes: The selection control structure
• Defines TWO courses of action depending on the outcome of a condition. A
condition is an expression that is, when computed, evaluated to either true or
false.

• The keywords used are if and else.

• Format:
if (condition) Example:
then-part
if (total_mark is greater than 50)
else print “Pass”
else
else-part
print “Fail!”
end_if end_if

Control Structures & Pseudocode


Pseudocodes: The selection control structure

• Sometimes in certain situation, we may omit the else-part.


if (number is odd number)
print “This is an odd number”
end_if

Control Structures & Pseudocode


Pseudocodes: The selection control structure

• Nested selection structure: basic selection structure that contains


other if/else structure in its then-part or else-part.
if (number is equal to 1)
print “One”
else if (number is equal to 2)
print “Two”
else if (number is equal to 3)
print “Three”
else
print “Other”
end_if

Control Structures & Pseudocode


Pseudocodes: The repetition control structure

• Specifies a block of one or more statements that are repeatedly


executed as long as a condition is satisfied.
• The keyword used is while.
• Format:
while (condition)
loop-body
end_while

Control Structures & Pseudocode


Pseudocodes: The repetition control structure
• Example: Finding odd value

while (value can be divided by 2 without remainder)


read new value
end_while
print the odd value

Control Structures & Pseudocode


Pseudocodes: The repetition control structure
• Example: Summing up 1 to 10

set cumulative_sum to 0
set current_number to 1
while (current_number is less or equal to 10)
add the cumulative_sum to current_number
add 1 to current_number
end_while
print the value of cumulative sum

Control Structures & Pseudocode


Pseudocodes: The repetition control structure
• Alternatively, we can write the previous pseudocodes with something like this.
• Example: Summing up 10 numbers
cumulative_sum = 0
current_number = 1
while (current_number ≤ 10)
cumulative_sum = cumulative_sum + current_number
current_number = current_number + 1
end_while

print the value of cumulative_sum

• Note that in this algorithm, we are using both the sequence and repetition
control structure

Control Structures & Pseudocode


Example - Pseudocode
• Design an algorithm of a program that adds number 1 to n, where n is provided by the user.
After adding, the program will print a message state if the sum is larger than 10 or not. Use
pseudocode.

Control Structures & Pseudocode


Conclusions
• 3 basic control structures : sequence, selection and repetition
structures
• Pseudocode is an informal language that is used to develop an
algorithm before programming

Control Structures & Pseudocode

You might also like