0% found this document useful (0 votes)
21 views7 pages

Control Structures

The document discusses control structures and flow of control in algorithms. It describes three basic control structures: sequence, selection, and iteration. It provides examples and definitions of each type of control structure.

Uploaded by

cnisijohn4
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)
21 views7 pages

Control Structures

The document discusses control structures and flow of control in algorithms. It describes three basic control structures: sequence, selection, and iteration. It provides examples and definitions of each type of control structure.

Uploaded by

cnisijohn4
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/ 7

CONTROL STRUCTURES / FLOW OF CONTROL

Control structures are instructions or statements that determine the flow of control of steps in
an algorithm.

The steps that are identified for preparing algorithms can be written using three basic or
program constructs to indicate how instructions will be carried out or how the flow of control
from one statement would take place. They are:
– Sequence
– Selection
– Iteration [Repetition]

SEQUENCE
The sequence control structure is used when you have instructions to be carried out in a
particular order. In an algorithm these instructions can be written in the order you want it to
happen, as follows:

Here, the instructions will be completed in the order given. This control structure can be used
for problems algorithm involving accepting inputs, performing calculations and storing them,
and displaying the outputs. The following is a typical example of an algorithm that uses the
sequence control structure where Steps 1 to 5 will be completed in the order given.

Step 1: Start
Step 2: Get two numbers
Step 3: Add the numbers and store it in Answer
Step 4: Display Answer
Step 5: Stop
SELECTION
The selection control structure is used in problems with instructions to be carried out if a certain
condition is met. The choice of options will be dependent on whether the condition is true or
false.

Selection control structure statements commonly used in algorithms are normally written as:
If <condition> then <instructions to be performed if the condition is true> else
< Instructions to be performed if the condition is false>

The following is an example of an algorithm with a selection control structure.


Step 1: Start
Step 2: Accept score
Step 3: If score more than 59 then display 'the student has passed' else display 'student has
failed'
Step 4: Stop

Miss John – GHS Problem solving 1


In the above example, if the score of a student is more than 59 it will display 'the student has
passed' and if it is less than 60 it will display 'the student has failed', which means only one of
the two messages can be displayed. If the condition is true [the score is more than 59), the part
after 'then' will be carried out, otherwise the part after 'else' will be carried out.

ITERATION OR REPETITION OR LOOP


Repetition/Iteration/Looping control structures are used to repeat a certain process a number
of times.

BOUNDED ITERATION is the process of repeating a set of steps or instructions a fixed number
of times. Suppose you want to accept 100 numbers from the user and find their sum, it would
be wise to repeat the process of getting the numbers 100 times rather than writing instructions
100 times to accept the numbers. Commonly used iteration statements in algorithms include
for-endfor.

UNBOUNDED ITERATION is the process of repeating a set of steps or instructions until a


particular condition becomes false. There are also situations where the process has to be
repeated until a specific condition becomes false. For example, find the sum of a set of numbers
terminated by 0. Here, getting numbers and finding their sum will be repeated until the user
supplies 0 as the value for the number. Repeating a set of steps, a number of times until a
particular condition becomes false is called unbounded iteration. Common unbounded iteration
statements are while-end while, and repeat-until.

How to create algorithms for problem statements


Now that you understand how algorithms are created and their components, let's look at some
problem statements and write algorithms for them.

Problem statement 1
Write an algorithm that will accept three numbers, add them together and print their sum.
1. First we gather the user values. In the problem statement, 'three numbers' is the input and
'sum' is the output required. 'Accept', 'add' and 'print' represent the processing that needs to be
done. Storage will take place when the calculated values of the three numbers are assigned to
sum.

You should also check the problem statement to see if any control structures are needed. Since
there is no need for decision making, the selection control structure is not required. Also, there
is no need for repeating processes, so the repetition stage is not required.

2. Now let's break down the problem into steps and write it in the form of single specific tasks to
form the algorithm:
Step 1: Start.
Step 2: Accept three numbers.
Step 3: Add the numbers entered.
Miss John – GHS Problem solving 2
Step 4: Display the sum.
Step 5: Stop.

Problem statement 2
Prepare an algorithm that will read the price of an item and display its new price after a discount
of 10% if the price is more than $100.
Problem statement 3
Create an algorithm that will read three numbers and find the largest among them.

Summary of key points


In this topic you have learned:
- How to write an algorithm to solve a problem statement.
Representing an algorithm using narrative pseudocode
In the previous topics, you learned that algorithms are step-by-step definitions of a task or a
problem. In this topic you will learn about how they are represented.

Algorithms can represented in three different ways, they are:


– Narrative
– Pseudocode
– Flowchart

Narrative, also called general algorithm, is where each step in the algorithm is written in clear,
simple language. It does not use any computer language or coding. This is similar to how you
have been writing algorithms so far.

For example, if you wanted to write an algorithm to read in three numbers then find and display
their sum, the narrative could be:
Step 1: Start.
Step 2: Get the three numbers.
Step 3: Add the numbers.
Step 4: Store the results in Sum.
Step 5: Display Sum.
Step 6: Stop.

Narrative is a representation of an algorithm where each instruction is written in everyday


language. As you can see, each instruction would be clearly understood by any person who
speaks English. Now let's look at some other examples of narrative.

Narrative algorithm 1
Write an algorithm in narrative form to read in three numbers from the keyboard and calculate
and display their product. Remember that, when given a problem statement, you first need to
identify the words that describe input, output and processing. So, in the above problem

Miss John – GHS Problem solving 3


statement, the inputs are the three numbers and their product is the output. The processing
involves accepting three numbers and calculating the product, which is multiplying the numbers.

So the narrative would be:


Step 1: Start.
Step 2: Get the three numbers.
Step 3: Calculate the product by multiplying the numbers.
Step 4: Store the results in Product.
Step 5: Display Product.
Step 6: Stop.

Narrative algorithm 2
Prepare an algorithm in narrative that will accept the length and width of a rectangle and
calculate and display its area. In the problem statement above, the inputs are the length and
the width of the rectangle and output needed is its area. The processing involves accepting the
values for length and width and calculating the area by multiplying the length and width. So the
narrative can be:

Step 1: Start.
Step 2: Get the length and the width.
Step 3: Calculate the area by multiplying the length and width.
Step 4: Store the results Area.
Step 5: Display the area.
Step 6: Stop.

Narrative algorithm 3
Create an algorithm in narrative that will prompt the user to enter the salary of an employee
and calculate the income tax at 15% if the salary is more than $5000. Display the salary and tax.

PSEUDOCODE
As you know, we create algorithms in preparation for giving instructions to the computer.
The instructions have to be in a form that is understandable to computers, so are written using a
programming language.

Narrative steps are very simple and useful for when you start learning about programming, but
when you are comfortable with programming you can create algorithms using instructions with
words and symbols that closely resemble computer programming language instructions. This
form of representation is called pseudocode.

The name 'pseudocode' comes from 'pseudo', meaning 'fake' and 'code' meaning program. In
pseudocode even though the terms used closely resemble programming language terms, they
are used without following the rigid rules of the language.
Miss John – GHS Problem solving 4
PSEUDOCODE is a way of documenting the solution to a problem using simple structured
instructions

VERBS USED FOR SPECIFIC TYPES OF ACTIONS

Arithmetic operators Operation


+ Addition
- Subtraction
* Multiplication
/ Division
Div A:=7 div 3The value of A is 2
Mod A: =7 mod 3 the value of A is 1

Relational operators -operators used to check for comparisons. The table below shows
commonly used relational operators and their operations.

Relational operator Operation


> Greater than or equal
< Less than
>= greater than or equal to
<= less than or equal to
<> not equal or equal to
= equal to

Logical operators -operators used to make comparisons with multiple criteria.


Logical operator Operation
AND And
OR Or
NOT Not

Miss John – GHS Problem solving 5


Just as in mathematics, in computing all the operations are carried out in a hierarchical order.
The computer follows the BODMAS rule. So anything in Brackets will be done first, followed by
Orders [such as powers and square roots), then Division and Multiplication.

Addition and Subtraction will be done last. So when you are writing instructions you must
ensure that you write them in the order in which you want the computer to carry them out.
PSEUDOCODE TERMINOLOGY
The general programming language terms used in pseudocode are:
• Terms used for the input step: input, read
• Terms used for the output step: output, write, display
• Terms used for the assignment step: set, store
• Terms used for selection: if-else-endif
• Terms used for iteration [bounded): for-endfor
• Terms used for iteration (unbounded): while-endwhile. repeat-until

Some programmers use terms in their pseudocode from other programming languages that they
are comfortable with. However, this can create problems for somebody who is not familiar with
that particular programming language. Generally it is wise to use terms like the ones given
above so that any person can follow the logic of the program.
PSEUDOCODE ALGORITHM 1
Write a pseudocode algorithm to find the average of three numbers.

For this example, let's look at the narrative first and then write the corresponding pseudocode
for it.
Narrative
Step 1: Start.
Step2: Get the three numbers
Step 3: Add the three numbers, divide by 3 and store the results.
Step 4: Display the results.
Step 5: Stop.

Pseudocode
Step 1: Start
Step 2: Read a [Accept the value for the first number and store it in a.)
Step 3: Read b [Accept the value for the second number and store it in b.)
Step 4: Read c [Accept the values for the third number and store it in c.)
Step 5: Set average (a + b + c) /3 [Add the three numbers and divide it by 3 and store the results
in average. The left arrow represents the assignment and it takes place from right to left
meaning after the calculations the results will be stored in variable average. Also note
that the brackets allow the addition of three numbers to take place before the division.)
Step 6: Write average [Display the results stored in the variable average.)
Step 7: Stop

Miss John – GHS Problem solving 6


Notice that in pseudocode, instructions are transformed into general programming language
terms with variables, constants and statements.

Pseudocode
Start
Read a
Read b
Read c
Set average = (a+b+c)/3
Write average
Stop

Pseudocode algorithm 2
Prepare a pseudocode algorithm that will accept the length and width of a rectangle and
calculate and display its area.
Narrative
Step 1: Start.
Step2: Get the length and width of the area
Step 3: multiply the length by the width and store the results.
Step 4: Display the area.
Step 5: Stop

Pseudocode
Step 1: start
Step 2: input length (Accept length and store it in variable length.)
Step 3: input width (Accept width and store it in variable width.)
Step 4: set area = length * width (Calculate the area by multiplying the values of length and
width and store the results in area.)
Step 5: output area (display the results stored in variable area.)
Step 6: stop
As you can see, the two examples above used the control structure sequence, so each
instruction will be carried out in the order it has been written. Now let's look at examples of
pseudocode where the control structure selection is used.

Pseudocode
Start
Input length
Input width
Set area = length * width
Output area
Stop

Miss John – GHS Problem solving 7

You might also like