Week 7 · Unit 7.2: Decomposing problems · 3 lessons
3
Hunt the bug with a flowchart
Computing 7: Unit 7.2, pp. 44–81
GoalFind and fix syntax errors and logic errors by comparing a program with its flowchart.
New words
bug · a mistake in a programdebugging · finding and fixing the mistakes in a programsyntax error · a mistake in the rules of the language, such as a misspelt command or a missing quotation marklogic error · a mistake in the plan: the program runs but does the wrong thing
Explanation
Debugging means finding and fixing bugs. In a text language it is easy to make a mistake. The best method is to put the flowchart next to the program and check the program line by line against the steps. A syntax error breaks the rules of the language, such as scrol, a missing quotation mark or a capital letter that should be a small one. The program will not work properly. A logic error follows the rules but does the wrong thing, such as using button A when the plan says button B, or putting two steps in the wrong order. Many editors change the colour of text in quotation marks, which is a quick way to see that a string is closed. Work in a pair: one reads the plan aloud, the other checks the program.
Kind of error
Example
What happens
syntax error
display.scrol("Hi")
a line breaks the rules, so it cannot work
logic error
button_a when the plan says button B
the program runs but does the wrong thing
Worked examples
This is the plan. When button B is pressed, the board shows a tick, waits two seconds and then scrolls the word Bye.
This program is meant to follow the plan, but it has bugs. Check each line against the plan. Line 4 shows a cross, but the plan says a tick. Line 5 waits 200 milliseconds, which is only 0.2 seconds, but the plan says 2 seconds.
when B is pressed: a tick for 2 seconds, then Bye scrolls
Here is the corrected program. Both bugs were logic errors, because the program ran but did not follow the plan. The right time is 2 seconds, which is sleep(2000).
Class activity
Bug swap (no computer). Choose a small job, such as “when A is pressed, scroll a greeting and show a heart”, and draw its flowchart. Write the program on paper. Then copy it out again and secretly add two bugs, one syntax error and one logic error. Swap with a partner. They must find both bugs by comparing your program with your flowchart, and say which kind each bug is. If you have a computer or tablet, an adult can help you check whether the corrected program runs.
Practice
1
The plan says: when A is pressed, scroll Hi and show a heart. Find the two syntax errors and write the correct lines.
Line 4: Hi needs quotation marks: display.scroll("Hi"). Line 5: heart must be capitals: Image.HEART. Both break the rules of the language, so they are syntax errors.
2
Say whether each bug is a syntax error or a logic error. (a) display.shwo(Image.NO) (b) the plan says button B but the code uses button_a (c) a closing quotation mark is missing (d) the plan shows a heart but the code shows a square.
The picture names are the wrong way round. The correct lines are display.show(Image.NO), then sleep(1000), then display.show(Image.YES). This is a logic error, because the program runs.
4
Why does numbering the lines of the program and the steps of the flowchart help when you debug?
You can compare one line with one step at a time, so you notice a line that is missing, in the wrong place or different from the plan. It also lets you tell your partner exactly where the bug is.