Sequencing activities

You can think of a program as similar to a list of instructions you provide to achieve some task you want performed. If you wanted to make a cup of instant coffee you might write a set of instructions along the lines:
  1. fill kettle with water
  2. switch kettle on
  3. get cup out of the cupboard
  4. get coffee jar out of the cupboard
  5. get teaspoon from cutlery draw
  6. put coffee in cup
  7. when kettle boils pour water into cup
  8. stir coffee
This list of instructions is not very precise but illustrates the concept of a 'program'. A very important aspect is the order in which the instructions are performed. The implicit assumption in this example is that the instructions are performed in the order they are written down. If we reordered the steps you can see that the outcome might not be quite the same. So, for example, if the kettle is switched on before being filled the result might be disasterous.

So it is crucial that we have some means or some principle by which the order of the execution of instrutions is defined. In executing a program Scratch, as you have seen, simply executes each Block in a stack in exactly the same order as they are found, moving from the top to the bottom of the stack.

It's quite common in the programming world to think of a counter being associated with the current position that execution has reached (this is perhaps because it is actually closer to how things take place at a much lower hardware and 'compiled' program level). So, at the start of execution the counter is set to zero and the first program statement (or Block) found at position zero (in the Stack) is retrieved and the action(s) it specifies are performed before the counter is increment and then used to retrieve the next instruction (Block) at position 1.

Open the same project you downloaded earlier (or download again) and then select the 'Edit' menu and the 'Set Single Stepping ...' option. Then select 'Flash blocks (slow)' in the pop-up dialogue box. Then if you run the program stack again you will see that each Block is highlighted as it is executed (see Figure 1 below for the first 3 states).

stepping through a program stack
Figure 1 Stepping through a program stack.

The concept of a progam counter is demonstrated by the stepping demo but we have also created another demonstration which illustrates the earlier example of making a cup of coffee.

This demonstration uses a longer program which you don't need to examine or understand. We want you to simply download, open and run the program.

When you run the program watch the stage area. You will initially see that there is a 'Program Counter' which is set to '0', a 'Current instruction' which is blank and a list of numbered instructions (Figure 2).

stage of the making coffee demonstration
Figure 2 Making coffee demonstration stage.

When the stack is run it increases the program counter and then uses the counter to retrieve the next instruction from the list of instructions until there are no more instructions. There is a 2 second delay between each increment of the counter so that the actions can easily be seen.

That concludes the activities which illustrate how a program is executed one step at a time.


Back