C3.2 Read and alter existing code, including code that involves sequential, concurrent, repeating, and nested events, and describe how changes to the code affect the outcomes.
Skill: Reading Code
Reading code involves interpreting blocks or commands to make predictions about its outcome. Reading code is also an important part of debugging to identify and correct errors of why the code is not working or getting the desired outcome.
For example, students can read the following code and identify the pattern:
Image Blocks of code: Events block stating “start on when green flag is clicked”. Pen extension stating “erase all”. Motions blocks stating “go to x:’-200’ y: ‘-160’.” Control blocks stating “wait ‘1’ seconds”. Pen extension stating “stamp”. Motions blocks stating “change y by ‘20’.” Control blocks stating “wait ‘1’ seconds”. Pen extension stating “stamp”. Motions blocks stating “change y by ‘20’.” Control blocks stating “wait ‘1’ seconds”. Pen extension stating “stamp”. Motions blocks stating “change y by ‘20’.” Control blocks stating “wait ‘1’ seconds”. Pen extension stating “stamp”. Motions blocks stating “change y by ‘20’.” Control blocks stating “wait ‘1’ seconds”. Pen extension stating “stamp”.(Move token), (Pause for one second), (Stamp)
Skill: Altering Code
One of the reasons to alter code is to save time by not starting from scratch but to use existing code to build something new. Altering code can also be done to make the code more efficient and reduce runtime or to demonstrate that there is more than on way to use code to represent a situation. Another reason to alter code is to fix errors and make it functional, a process called debugging.
The following code is an alteration of the above code in which a loop is used to make the code more efficient.
Image Blocks of code: Events block stating “start on when green flag is clicked”. Looks block stating “show”. Pen extension stating “erase all”. Motions blocks stating “go to x:’-200’ y: ‘-160’.” Controls blocking “repeat 5”. Inside 3 nested blocks. Control blocks stating “wait ‘1’ seconds”. Pen extension stating “stamp”. Motions blocks stating “change y by ‘20’.” Looks block stating “hide”.Skill: Describing the Impact of Altering Code
Understanding the sequencing of events that will occur based on the code is important for altering code so that one can make predictions about how the changes sill affect the outcome.
Example
Given the following code:
Image Blocks of code: Events block stating “start on when green flag is clicked”. Pen extension stating “erase all”. Pen extension stating “pen down”. Motions block stating “go to x: ‘0’ y” ‘0’”. Control block stating “repeat 10”. Inside 2 nested blocks. Control blocks stating “repeat 2”. Inside 4 nested blocks. Motions block stating ‘move 100’ steps”. Motions block stating “turn left ‘90’ degrees”. Motions block stating ‘move 150’ steps”. Motions block stating “turn left ‘90’ degrees”. Motions block stating “turn right ‘15’ degrees”. Pen extension stating “pen up”.The following pattern is created:
Image This is a series of ten rectangles. The starting point of each rectangle is the same. The lower left corner of each rectangle is joined to the center point. This formation gives the appearance of a flower.The following questions could be asked of the student:
- What alterations to the code will be needed to create triangles instead of rectangles?
- What will happen if the number of degrees of rotation increases towards the end of the code?
- Is the "raise the pen" block necessary? What is its role?
- How will more repetitions of the outer loop change the appearance of the pattern?