Week 27 · Unit 7.6: Sequencing and pattern recognition · 3 lessons
3
Plan it in pieces
Computing 7: Unit 7.6, pp. 182–224
GoalUse a sub-routine in a flowchart and write a test plan before you write the program.
New words
sub-routine · a named mini-program kept apart from the main program and run by using its nameproject plan · a plan that says what a program must do, its sections, inputs and outputs, and how it will be testedtest plan · a table of tests with the test data, the expected outcome and the pass or fail result
Explanation
A sub-routine is a named mini-program that is kept apart from the main program. The main program runs it just by using its name, and then carries on. In a flowchart the real symbol is a box with double lines at the sides. In these drawings it is a box that says run sub-routine. Sub-routines make a flowchart shorter and easier to read, and each small section can be tested on its own. Before writing code, the programmer makes a project plan and asks questions. Which button is the input? What is the output? What happens when no button is pressed? Then a test plan is written, and each expected outcome is decided before the test is run. The same test plan is used again later to test the finished program.
Worked examples
This is the sub-routine called Welcome. The main flowchart runs it when B is pressed. A lamp over a shop door shows a heart for 1 second and then clears the screen.
Test
What is tested
Test data
Expected outcome
Pass/fail
1
B starts Welcome
B pressed
a heart is shown
pass
2
Welcome ends
B pressed
after 1 second the screen is clear
pass
3
Idle state
no button pressed
the screen is clear
pass
This is the test plan for the shop-door lamp. The expected outcomes were written before testing. After testing, the results were written in the last column.
Part of the plan
Diego’s answer
What it must do
show a welcome light when a visitor presses the button
Sections
the main program and the sub-routine Welcome
Input and output
input: button B; output: the heart on the lights
How to test
the three tests in the test plan
Use the results
fix any failed test, then ask the shop owner what to change
The project plan breaks the job into smaller questions. Here is Diego’s plan for the lamp.
Class activity
Flowchart relay (no computer). Work in a team of three. Player 1 draws a main flowchart that runs a sub-routine when A is pressed and otherwise shows a clear screen. Player 2 draws the sub-routine: two pictures with waits. Player 3 writes a test plan with three tests, and decides the expected outcomes before anyone tests. Then Player 1 plays the button presses while Players 2 and 3 follow the flowchart and mark pass or fail.
Practice
1
Follow this flowchart. (a) Which input runs the sub-routine Sunset? (b) What happens when both buttons are pressed together? Explain. (c) What happens when no button is pressed?
(a) Button B, but only when A is not pressed. (b) Only Sunrise runs. The flowchart asks about A first, and when the answer is yes the question about B is never asked. (c) The screen is cleared.
2
Sunrise shows a small diamond for 1 second, a diamond for 1 second and then clears the screen. Sunset shows a diamond for 1 second, a small diamond for 1 second and then clears the screen. Copy this test plan for the flowchart in question 1 and write the three expected outcomes by hand.
Test
Test data
Expected outcome
Pass/fail
1
A pressed
?
2
B pressed
?
3
A and B pressed together
?
Test 1: Sunrise runs: a small diamond for 1 second, a diamond for 1 second, then a clear screen. Test 2: Sunset runs: a diamond for 1 second, a small diamond for 1 second, then a clear screen. Test 3: only Sunrise runs, the same as test 1, because A is asked first.
3
Ayla is making a night-light for her little brother. Pressing B starts a pattern. Write the first three questions for her project plan.
Model questions: Which button is the input (B)? What will the pattern look like and how long will it last? What must the light do when B is not pressed? Other good questions: how will it be tested, and how can the light be turned off?
4
Explain two reasons why a flowchart with sub-routines is easier to understand and test.
The main flowchart is shorter, because the details are hidden inside named boxes, so it is easier to follow. Each sub-routine is a small section that can be tested on its own, and a mistake in one section is easier to find.