C3.2 Read and alter existing code, including code that involves conditional statements and other control structures, and describe how changes to the code affect the outcomes and the efficiency of the code.
Activity 1: Coordinates
Present students with the following code:
Ask students to explain what is happening.
- Using pseudocode, explain what is happening.
- What are the coordinates of the shape above?
Possible pseudocode to explain the visual:
Start at coordinate (0, 0)
Activate the "pen".
Advance 100 steps, rotate 45 degrees.
Advance 120 steps, rotate 135 degrees.
Advance 100 steps, rotate 45 degrees.
Advance 120 steps, rotate 135 degrees.
It is also possible to use the approximate coordinates in the pseudocode instead of the angles.
Start at coordinate (0, 0)
Activate the "pen".
Go to (100,0).
Go to (180, 80).
Go to (80, 80).
Go to (0, 0).
Code leading to visual to show students:
Present students with the following code:
If we want to translate 100 units to the left and 100 units down, what change should we make to the code?
Possible student responses:
- We need to go from the point (0, -100) to the point (100, 0).
- We need to change the angle at which the sprite will turn to the point (0, -100).
- We must use the additional angle to the one used to make the code (instead of turning 135 degrees, we must turn 45 degrees).
Code leading to visual to show students:
Activity 2: Geometric Shapes
Introduce students to the code below and ask them to explain what is happening using pseudocode.
Possible pseudocode:
At the start, the sprite goes to (0, 0) and moves to the right.
All pen marks are erased and the pen is in writing position.
Repeat 4 times
Advance (30 steps)
Turn (90 degrees)
The result will be a square: 4 congruent sides, 4 angles of 90 degrees.
Ask students to alter the code so that it draws an equilateral triangle.
Possible questions
- How many sides will a triangle have? (3, so you will have to change the number of repetitions in the loop.)
- What are the interior angles' measurement of an equilateral triangle? (60 degrees, so you will have to change the value of the "turn" block.)
Please note! The interior angles are 60 degrees, but it is possible that the chosen coding program interprets the angle as an exterior angle. If this is the case, the program will have to use 120 degrees and not 60 degrees to make the equilateral triangle. This can become a rich mathematical conversation.
Use the same questioning to get students to alter the code to create a hexagon.