Programming. Controlling program flow in plain English.
John McGuinn's Programming Tutorial

Controlling Program Flow

New 19 Feb. 2001
 

Contents

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.

All programs can be written using these very simple constructs.

  1. Sequence
  2. Repetition (aka iteration or Loops)
  3. Conditional Branching
  4. 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!

  1. Fill the kettle
  2. Boil water
  3. Coffee in cup
  4. Pour on water
  5. Add sugar
  6. Add milk

Assume that we are now going to make 2 cups of coffee. This could be achieved with the following sequence of instructions

.

  1. Fill the kettle
  2. Boil water
    
    	 
  3. Coffee in cup
  4. Pour on water
  5. Add sugar              
  6. Add milk
    
      
  7. Coffee in cup
  8. Pour on water
  9. Add sugar              
  10. Add milk   

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.

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:

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

> 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:-

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

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

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

| Top of Page |