C3.1 Solve problems and create computational representations of mathematical situations by writing and executing efficient code, including code that involves events influenced by a defined count and/or subprogram and other control structures.

Skill : Solving Problems in a Computational Way


Coding can be used to automate tasks and visualize mathematics to facilitate problem solving. By its very nature, coding lends itself well to trial and error, giving students the opportunity to solve problems by learning from their mistakes. Therefore, the student can use the questioning "what will happen if…?".

For example, the student might ask how measures of central tendency in a set of data compare. The student could first create subprograms in their code that would execute certain commands or blocks to determine the mean, median, mode, and range of the set of data. The subprograms could later be integrated into the main program.

Skill : Representing Mathematical Situations in a Computational Way


Coding can be used as a representational tool in the same way as manipulatives. By using written blocks or commands, very complex mathematical situations can be modelled and manipulated visually, which can make very abstract concepts concrete.

For example, to calculate the mean of a set of data, the student will need to be familiar with how to perform this calculation in order to choose the right commands or blocks to achieve this goal. Here is an example of code that would calculate a mean:

Blocks of code:Events code stating, “when this sprite clicked”.Variables block stating, “set list number to 1”.Variables block stating, “set sum to 0.”Control block stating, “repeat’’ list block stating, “length of list of shoe sizes”.”Inside 2 nested blocksVariable block stating, “change sum by’’ list block stating, “item list number of list of sizes”.”Variables block stating, “change list number by one.”Looks block stating, “say join the average shoe size for the class is sum divided length of list of shoe sizes.”

In this example, the calculation of the mean is represented, in the last line of code, as the sum of the data divided by the length of the list.

Skill: Writing Efficient Code


Writing or editing efficient code involves placing instructions in a specific order, following the syntax of a programming language and using a minimum number of blocks or commands to achieve the desired outcome. Writing code can be similar to writing text. Pseudocode writing, on the other hand, involves writing code instructions in the familiar language. Block-based coding can make it easier to follow syntax by using different block shapes and colours.

For example, some code will use very complex sequences that, in the body of the main code, may represent cluttered and difficult-to-read code. Using a subprogram allows you to maintain some structure free of the main code by calling on the complex sequences when the need arises.

Here is an example of a subprogram that has been transformed into a custom block.

My list stating, “define data collection”.Control block stating, “repeat until answer equals no”.Inside 4 nested blocks.Sensing block stating, “ask what is the students show size, question mark and wait.”Variables block stating, “set shoe size to’’ motions block stating, “answer”.List block stating, “add’’ variable block stating, “shoe size to list of show sizes.’’ Sensing block stating, “ask do you have another data value to enter, question mark. Open parenthesis, yes or no, closed parenthesis, and wait.”

Once this block is created, the main code would look like this:

Blocks of codeEvents block stating, “when green flag is clicked.”List blocks stating, “delete all list of shoe sizes.”My list stating, “data collection.”

There are many ways to call a subprogram, and the software or language in which the code is written will affect this process.

Skill: Executing Code


Code execution is the step where the code sequence is read and compiled by the computer. It is at this stage that a functional code will give the desired outcome (and a non-functional code will give a different outcome or no outcome). In block-based coding, code execution is often done by means of a button in the interface, whereas text-based programming languages require precise compilation software that essentially translates the code from the programming language to the machine language (for example, binary code) so that the machine can execute the code. This also applies to robotic devices.

Some examples of run buttons for block-based coding software

Examples of coding execution button.

Run buttons in MakeCode

Examples of coding execution button.

Run buttons in Scratch

Examples of coding execution button ‘run’.

Python compilation software "Programiz" run button

Knowledge: Subprograms


A subprogram is a small set of instructions that performs a simple task outside the main code. Subprograms can be combined in a main program to accomplish a large task using simple steps.

For example, it is possible to create a custom block that, when executed, calls a series of specific instructions (a subprogram). The first step is to create a block with two adjustable parameters (for example, base and height). In this case, the "rectangle" block sends the message to start executing the code below the "define: rectangle" block. This ensures that a rectangle of any size can be generated anywhere on the canvas, because the rectangle has been defined. Once the rectangle has been drawn, the main code continues.

My blocks stating, “define rectangle base height.”Pen extension stating, “erase all.”Pen extension stating, “pen down.”Variable block stating, “repeat 2.”Inside 4 nested blocks.Motions block stating, “move’’ my list blocks stating, “base” steps.”Motions block stating, ‘’turn right 90 degrees.”Motions block stating, “move my list blocks stating, “height” steps.”Pen extension stating, “pen up.”

To insert the rectangle, simply add the new "rectangle" block to the code, and specify the base and height values.

Block of code: My list block stating, “rectangle.”

Note: The way to access such subprograms depends on the software or the language chosen.

Knowledge: Events and Structures (Prior Knowledge)


Sequential Events

A set of instructions executed one after the other, usually from top to bottom or from left to right on a canvas.

Example

Blocks of code:Start on green flag, move down one time 5 times, move right one time 2 times.

Concurrent Events

Several events that occur at the same time.

Example

Repeating Events

Something that happens over and over again. In coding, loops are used to repeat instructions.

Example

Blocks of code:Events block stating, “when green flag clicked.”Control block stating, “repeat 4”.Inside 2 nested blocks. Motions block stating, “move 60 steps”. Control block stating, “create clone of myself”.

Nested Events

Control structures placed inside other control structures. For example, loops appearing inside loops, or a conditional statement being inserted inside a loop.

Example

Blocks of code:Control block stating, “repeat 3”.Inside 1 nested block.Controls block stating, “repeat 4”.

Conditional Statements

A type of coding instruction that tells the computer to compare values and expressions, and to make decisions. A conditional statement tells a program to perform an action based on whether the condition is true or false, often using the command IF, THEN, and ELSE.

Example

Blocks of code:Control block stating, “if base, equals, height, then.”Inside two nested blocks.Looks block stating, “say yes, coma, its a square, exclamation mark.”Looks block stating, “say no, coma, its not a square, exclamation mark.”

Code Efficiency

Writing functional code using a minimum of blocks or commands.

Example

Blocks of code:Events block stating, “when green flag clicked.”Pen extension stating, “erase all”.Pen extension stating, “pen down”.Motions blocks stating, “move 200 steps”.Motions block stating, “turn left 90 degrees”.Motions blocks stating, “move 200 steps”.Motions block stating, “turn left 90 degrees”.Motions blocks stating, “move 200 steps”.Motions block stating, “turn left 90 degrees”.Motions blocks stating, “move 200 steps”.Motions block stating, “turn left 90 degrees”.Pen extension stating, “pen up”.Big blue arrow points to second block of codes:Events block stating, “start on when green flag clicked.”Pen extension stating, “erase all”.Pen extension stating, “pen down”.Variables block stating, “repeat 2.”Inside 4 nested blocks. Motions blocks stating, “move 200 steps”.Motions block stating, “turn left 90 degrees”.Motions blocks stating, “move 200 steps”.Motions block stating, “turn left 90 degrees”.Pen extension stating, “pen up”.