| Programming. Controlling program flow in
plain English. |
|
John McGuinn's Programming Tutorial
|
Controlling Program Flow
New 19 Feb. 2001 |
| |
Contents
- Controlling Program flow - Overview
- Sequence
- Repetition (aka iteration or Loops)
- Conditional Branching
- Combined Branching and Repetition
- Conditional test at start of the construct
- Conditional test at the end of the construct
Controlling Program flow - Overview
This tutorial is going to look at simple instructions for making a cup of
coffee. These instructions are written in plain English, and how they are
arranged into steps so that the steps can be converted to code.
The steps have been deliberately made simple. E.g. Fill the kettle. This
Step can be broken down into many sub steps. Fill through spout, or lift lid
and fill. Take kettle to sink. Place under tap. Turn on tap. Etc. etc.
- This tutorial looks at how the flow of execution of the instructions,
within a program, takes place.
- The following principals are true to the majority of programming
languages.
- Thus learning a second programming language is usually easier than
learning the first one. You do not have to re-learn these control principles.
- The actual code that is required to execute these principals is
covered in individual language specific courses.
All programs can be written using these very simple constructs.
- Sequence
- Repetition (aka iteration or Loops)
- Conditional Branching
- Combined Branching and Repetition
Most programming languages also have more complex constructs that when
learnt make life easier for the programmer.
Sequence
When you start to learn programming, the initial programs are very simple
and usually consist of a sequence of instructions that are executed one after
the other.
A sequence of instructions is a list of instructions, which are executed
in the order in which they are written.
I will illustrate this using a example of writing a few simple
instructions for making a cup of instant coffee that includes sugar and milk!
- Fill the kettle
- Boil water
- Coffee in cup
- Pour on water
- Add sugar
- Add milk
Assume that we are now going to make 2 cups of coffee. This could be
achieved with the following sequence of instructions
.
- Fill the kettle
- Boil water
- Coffee in cup
- Pour on water
- Add
sugar
- Add milk
- Coffee in cup
- Pour on water
- Add
sugar
- Add milk
- You are probably thinking that you would not make 2 cups of coffee in
this order, please be patient. We will work towards a better human way shortly.
- It can clearly be see that instruction 7 - 10, are identical to
instructions 3 - 6.
- Note how the use of the blank lines (white space) helps to make this fact
stand out, and makes the instructions a little easier to follow. Remember to
use white space in your programs.
In the next section I will now show you a better way of writing these
instructions. Reusing the code that is in steps 3 - 6
Repetition (aka iteration or Loops)
From our example above a better method is to introduce an instruction that
will iterate though some of our previous sequential instructions, and create a
loop of instructions.
- A loop allows the repeated execution of 1 or more instructions.
- The example below will cater for any reasonable number
of people.
- A suggestion of carrying on with the serving of biscuits is included. To
demonstrate the white space principle to improve the readability.
1. Fill the kettle
2. Boil water
3. Count people requiring coffee:
4. Store answer in a variable CoffeeCount
.
5. Repeat following steps, value of CoffeeCount number of times
6. Coffee in cup
7. Pour on water
8. Add sugar
9. Add milk
10. End Repeat Construct
.
11. Count people requiring biscuits etc. .....
Note:
- Because the new code has additional steps, steps 3-6 have become steps 6 -
9 and in a bold black font for easy recognition.
- The Control Structure Steps 5 and 10 are also repeated and are in
bold blue font.
- How I have used white space to emphasise the loop by leaving a blank line
before and after the loop.
- Additional white space by indenting the instructions that will be iterated.
- That we must state exactly which instructions are going to be included
within the loop.
- This is usually achieved by stating the start and end of the loop. When
coding how this is done depends on the programming language, and control
structure, used. Standard methods of doing this is either :-
- Using a pair of marker symbols, one for the start and
one for the end of the loop. E.g.
- [ repeated code
] in Smalltalk.
- { repeated code
} in C and C++
- Or a pair of instructions similar to those used in the
steps:- Repeat
. End Repeat
- or a combination of both.
- A variable, CoffeeCount, is used, steps 4 and 5 the variable . Step 4 would
obtain the value, 2 in our example. Then step 5 would use this value, 2, and
repeat the loop twice.
- That the above instructions would also work for a single cup of coffee.
Work out what is happening, i.e. in what order is each line being
executed.
Question 1.. You may be thinking that
you would make your 2 cups of coffee differently. I.e. by putting coffee into 2
cups, pouring the water into both cups, adding milk and sugar to them both.
Rewrite the loop section starting line 5, so that the coffee will be made in
the manner described.
Go to Answer 1.
Note how the solution given is working. Assuming that the value of CoffeeCount
is still 2. Then all the code that is bold will be repeated twice, before the
code in the next section is carried out
Conditional Branching
Selection allows the program to branch and either execute an instruction or
group of instructions, or not execute those instruction(s).
In our example not everybody will require sugar, so we could refine the line to
the following
Add sugar
to
Do you require sugar?
If answer is yes
Add sugar
End If construct
- The instruction "Add sugar" would only be carried out the answer
to the question Do you require sugar? is yes.
- The action of adding sugar depends on the result of a Boolean condition.
- Boolean condition results can be one of 2 possible answers. The main ones
depending on the programming language are:-
- true or false.
- 0 or 1.
- There are variants to actual numbers used.
- yes or no.
1. Fill the kettle
2. Boil water
3. Count people requiring coffee:
4. Store answer in a variable CoffeeCount
.
5. Repeat following step, value of CoffeeCount number of times
6. Coffee in cup
7. End Repeat Construct
.
8. Repeat following step, value of CoffeeCount number of times
9. Pour on water
10. End Repeat Construct
.
11. Repeat following steps, value of CoffeeCount number of times
.
12. Do you require sugar?
13. If answer is yes
14. Add sugar
15. End If construct
.
16. End Repeat Construct
.
17. Repeat following step, value of CoffeeCount number of times
18. Add milk
19. End Repeat Construct
.
20. Count people requiring biscuits etc. .....
Notes
- Step 12 is the condition, which is
tested in step 13.
- The control start (step 13) and end (step 15) of the Conditional Branching
construct have been coloured maroon to aid recognition.
- The Add sugar step is indented within its own control steps.
- The pairing of start and end instructions can easily be seen by the white
space, and indentation. Pay particular attention to the vertical line up of the
start and end lines.
- The complete Add sugar construct is nested within a Repeat construct, hence
the additional indentation.
- For each start there should be a corresponding end.
- In complicated constructs nested to several levels, missing a single end
line can be difficult to spot.
- Placing a suitable comment against each start and end can help you
recognise the pair.
- Some languages allow a short cut one line exception to
this rule.
> Care must be taken the answer is 0 (zero) to the question how many people
require a cup of coffee because some loop instructions will always execute the
loop at least once, and this may be an undesirable result. This depends on the
programming language used, and the actual instruction used. Look for
information on this possible problem. And remember to test loops with 0 to
ensure that you do not receive unexpected results.
Combined Conditional testing and Repetition
Alternatives:-
- Some conditional branching constructs combine with a loop construct.
- The conditional test may be at the start or at the end.
Conditional test at start
1.
Some condition
2. Repeat while the condition is true
3. Code to be repeated
4. Some condition
5. End Repeat Construct
- Step 1. This step is only executed once, the white space can be placed in
front of the "Some condition" or after it.
- The loop will not be executed if the condition is false.
- The loop will repeat while ever the condition remains true.
- This creates a problem --- How to end the loop.
- Additional code is required within the loop that allows the condition
result to change. So that the loop will terminate. 2 possible ways of doing
this :-
- Incrementing / decrementing the value of a variable used in the condition.
- Obtaining user input, that will alter the value of a variable in the
condition.
- The additional same condition, step 4, within the loop, is for the
following test, step 2.
Conditional test at the end the construct, e.g.
1. Start loop
2. Code to be repeated
3. Some condition
4. Repeat while the condition is true
- The loop will always be executed at least once, think what you could do if
this is a problem.
- The loop will repeat while ever the condition remains true. See above for
preventing problems.
Programming languages.
The main words, words with the following as a prefix, or part of words, to look
out for in the various programming languages, that are connected to these
control structures are:-
if, then, else, while, repeat, until, with, do, for, loop, true, false, switch,
case, default, end, exit, continue, break
Answers
Answer Question 1.
1. Fill the kettle
2. Boil water
3. Count people requiring coffee:
4. Store answer in a variable CoffeeCount
.
5. Repeat following steps, value of CoffeeCount number of times
6. Coffee in cup
7. End Repeat Construct
.
8. Repeat following steps, value of CoffeeCount number of times
9. Pour on water
10. End Repeat Construct
.
11. Repeat following steps, value of CoffeeCount number of times
12. Add sugar
13. End Repeat Construct
.
14. Repeat following steps, value of CoffeeCount number of times
15. Add milk
16. End Repeat Construct
.
17. Count people requiring biscuits etc. .....
Return to Question 1