Control Structures
Control Structures
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>
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.
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.
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 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
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
Relational operators -operators used to check for comparisons. The table below shows
commonly used relational operators and their operations.
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
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