Algorithm
Algorithm
Or
answer is output
Again, it is important to note the indents and the fact that ELSE is immediately in line
with THEN. The statement below THEN is in line with the statement below ELSE.
This algorithm allows you to type in your grade as a word. The values of
X and Y are set accordingly depending on the word you type in. The range
of marks that your mark must have been within is then printed out after
the phrase
‘Your mark must have been between’. Note the comma after the
quotation marks. Commas are used to separate variable names from the
words that are printed out.
For example, if ‘distinction’ is typed in, then X is set to 75 and Y is set to 100
and the algorithm jumps immediately to ENDCASE and then prints out the
message: ‘Your mark must have been between 75 and 100’.
Notice neither the quotes nor the commas are printed out.
If the word ‘merit’ had been typed in then the first case (‘distinction’) is ignored
and the second case is carried out, then it jumps to ENDCASE and prints out
the appropriate message. With ‘pass’, it goes straight to the third case then
ENDCASE before printing the message, and with ‘fail’, it goes to the fourth
case then ENDCASE and prints out the message.
Loops
Loop is a sequence of repeated statements that are carried out a number of times.
There are two ways a loop operates.
o One way is sometimes referred to as a pre-condition loop, meaning the conditional
statement is checked before (pre) any of the sequence of statements is followed.
This is when a condition is checked to see if it is met and if it is, the sequence
of statements is carried out. When the last statement is reached, the conditional
statement is returned to. The condition is checked again and the whole process
is repeated. Once the condition has not been met, it jumps to the last statement
and exits the loop.
The other type of loop is sometimes referred to as a post-condition loop.
the conditional statement is only checked after the sequence has been carried out for
the first time. If the condition is not met, then the first
statement is returned to.
This is repeated until the condition is met, in which case the next statement after the close of
the loop is carried out. The loops which will be described here are the WHILE…ENDWHILE
loop (a precondition loop) and the REPEAT…UNTIL loop (a post-condition loop).
REPEAT…UNTIL
Unlike the WHILE…ENDWHILE loop, which tests the condition at the start
of the loop, the REPEAT…UNTIL loop checks the condition at the end of the
loop. It is similar to a WHILE…ENDWHILE loop, except that a REPEAT… UNTIL loop is executed
at least once.
despite the fact we might not want it to. This is a big disadvantage of the
REPEAT…UNTIL loop, but as long as you want the loop to be executed at
least once, it works well.
Nested loops
A nested loop, as its name suggests, is a loop within a loop. Let us start with an
example. We have already seen an algorithm which can output a multiplication table.
Now we will look at an algorithm which will output several multiplication tables.
The variable number1 is the number of multiplication tables we want and
number2 is how far we want the table to go up to. For example, if 6 is input for
number1, then the 1, 2, 3, 4, 5 and 6 multiplication tables will be printed out,
and if 12 is input for number2 each table will go up to 12×.
Notice that both count1 and count2 are incremented immediately after the loop
starts to prevent the algorithm from printing out 0×1 or 1×0. The WHILE…
statement only checks whether the condition is met before the loop is allowed
to start. When count2 gets to 6, it will have already printed out 1×6 and the
count1 loop will increment then count2 will be set back to 0. This is repeated
until count1 is 12 and count2 is 6.
The same output would be produced using the following nested REPEAT…
UNTIL loop:
Procedures/subroutines
A subroutine is a sequence of algorithm statements that perform a
specific task. This subroutine can then be used in other algorithms as and when
needed.
The subroutines or procedures follow the following pattern:
Notice that although we used length and width as the variable names when we set
up the rectangle procedure, we can use different variable names when we ‘CALL’ it.
As long as we ‘pass’ two numbers to the procedure, it will perform the calculation.
Once the procedure or subroutine is set up, it can be called as many times as we
want.
File handling
Let us consider the very basic algorithm that was constructed in Chapter 1.
You should now be able to convert it into a properly structured algorithm such
as this (notice we are using command words for the file input – READ and
output – WRITE):
Drawing flowcharts
A program flowchart is a diagrammatic way of representing an algorithm used to
produce the solution to a problem.
A flowchart consists of symbols, connected to each other by arrows which indicate
the path to be followed when working through the flowchart.
Each different shape symbol represents a different stage in the process. From now on
when we mention the word flowchart we are referring to program flowchart.