What a program is

Instructions, one at a time, top to bottom. Running code and reading what comes back.

F1.1Programming basicsGCSE10 min

Do this lesson in the simulator

New to the site? How it all works is a ten-minute tour of the lessons, the tasks, the competitions and every button on the screen.

This lesson has almost no typing in it. It is about the idea underneath everything else: what a program is, and what happens when you press Run.

A program is a list of instructions

A recipe is a list of instructions for a cook. A program is a list of instructions for a computer. The robot in front of you has a small computer inside, and it will do exactly what your instructions say, in the order you wrote them, and nothing else.

Here is a program. Do not change anything yet; just press Run and watch what happens in the box under the code.

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

print("one")
print("two")
print("three")

Run this in the simulator

Three lines of text appeared, in the same order as the three instructions. That box is the console: the robot talking back to you. On a real BugBot it is text the robot sends over the USB cable to your screen.

The first two lines

Every BugBot program starts the same way:

from bugbot import *
connect()

Each line does one job.

  • from bugbot import * brings in the robot's commands, so you can write forward(60). This is ordinary Python: it is how you use a library someone else has written.
  • connect() finds the robot and opens the link to it. In the simulator the robot is already there, so it happens instantly. With a real BugBot on the desk, this is the moment it wakes up.

Leave either line out and Python will tell you: without the first, the commands do not exist; without the second, there is no robot to send them to.

One at a time, top to bottom

The computer reads the first instruction and carries it out. Then the second. Then the third. It never skips ahead and it never goes back on its own. This is called sequence. Predict what this program prints before you run it, then run it to check:

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

print("start")
print("middle")
print("end")

Run this in the simulator

Now swap the print("middle") and print("end") lines (click into the code: it is an editor) and run again. The output changes to match. The order of the lines is the program.

Instructions can change the world

Printing only talks. Some instructions do things. Run this one and watch the robot on the mat:

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

print("moving")
# drive forward at 50 (keeps going until the next command)
forward(50)
# pause 1 s (the robot keeps doing what it was told)
wait(1)
# all motors off
stop()
print("stopped")

Run this in the simulator

The robot drove for one second. Notice when the words appeared: "moving" before it moved, "stopped" after. Same rule: one instruction at a time, top to bottom.

Running again

Press Run on the cell above a second time. The robot starts from the beginning again. A program does not remember what happened last time it ran; every run is a fresh start. On the real robot, that is what happens when you press its button.

Change the number 1 to 3 and run. Then press the reload button at the top of the cell: it puts the lesson's code back and clears the run. You can always get back to where a lesson started.

What you know now

  • A program is instructions for the computer, carried out one at a time, top to bottom.
  • Some instructions talk (print), some act (forward, stop).
  • Running starts from the first line every time.

Task: your first program

Make the robot print three lines. One of them must be exactly I am a BugBot. The robot should not drive.

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

print("...")

Challenges

  1. Print five lines that tell a very short story.
  2. Make the robot say "go" before it moves and "stop" after it stops.
  3. What happens if you put a print line before the first line? Try it and read what the robot says.