C3.2 Read and modify given codes, including codes with conditional statements and other control structures, and describe the impact of these changes on the results.

Skill: Reading Code


Reading code involves interpreting blocks or commands to deduce their meaning. This allows the student to make predictions about the behaviour of the code. Reading code is also an important part of debugging, which is the identification and correction of errors in the code, because by reading the code it is possible for the student to determine the reason(s) why it is not working. Note that we are not talking about the computer reading the code, but rather the student reading the code and checking it.

For example, when the student encounters a conditional statement while reading code, he or she must interpret this as a contingency in the code that underlies an action. There is then more than one possibility related to the execution of the code. In cases of more complex code, it may be beneficial to combine reading code with writing a flowchart to ensure a clear understanding of what is being read.

Skill: Modifying Code


The culture of computer programming is one of sharing, and many programs are created based on existing code. Modifying code first allows the student to start from an existing and functional code sequence in order to build something new. Modifying code can also be done to simplify it, or to demonstrate that there is more than one way to represent the same situation with code. If necessary, the student can also modify erroneous code to make it functional, a process called debugging.

For example, the student could start with a code that makes a sprite move around the screen. The student's job would be to create interactive elements when the sprite comes into contact with other objects. Here is an example of a possible interaction:

Blocks of code:Controls block stating “if” motions block stating”touching sun, question mark” “then”.”Inside one nested block.Looks blocks “say ‘ouch, exclamation mark, It’s hot, exclamation mark”.

Skill: Describing the Impact of Changes on Code


As much as trial and error is an important part of creating code sequences, it is important that students can justify their choices of changes to a code by making predictions about the effect of the changes on the behavior of the code.

For example, it is possible to represent a situation in which the theoretical probability of getting "heads" or "tails" by tossing a coin would be determined.

In this context, there are two possibilities, either "heads" or "tails". These two possibilities can be associated with two numbers, 1 and 2, and generate random results that are then compiled into an array. The pseudocode might look like this:

Variables

Battery (set to 0)

Face (set to 0)

Throws

Result

Ask "how many pitches do you want to generate?"

Throws = Response

Repeat ("Throws") times

Set "Throws" to (random number between 1 and 2)

If "Answer" = 2

Then add 1 to Tails

Otherwise

Add 1 to Face

Following the reading of this pseudocode, the student could be asked questions such as:

  • What if, instead of 1 and 2, the result options were 2 and 4?
  • How could I use two "if-then" commands instead of one "if-then-else" command?
  • What would be the impact of removing the "set to 0" commands at the beginning of the code?