C3.1 Solve problems and create computational representations of mathematical situations by writing and executing efficient code, including code that involves conditional statements and other control structures.

Skill: Solving Problems Using Computational Methods


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 question "what will happen if…?".

In addition, the question "what will happen if…?" allows one to start a conversation about conditions and conditional statements. For example, in a probability problem, one might ask "what will happen if I roll a die 1000 times?" or even "how many times will I get 2 by rolling a die 1000 times?" Given the time required to perform this experiment manually, code becomes an efficient way to solve such a problem.

The student will also need to think about how to use commands or blocks efficiently, namely, using as few as possible to achieve their purpose. A question like this one, requiring a large number of repetitions, could become a never-ending code if the right commands or blocks are not used.

Ability to Represent Mathematical Situations Computationally


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, it is possible to represent a mathematical situation in many ways using code, but some representations are more effective than others. Block-based coding software might, for example, help the student generate a list of data to identify its range, but a spreadsheet can accomplish the same task with less code. The student might first progress from a less efficient, but more visually understandable, representation to one that is more efficient, but has more abstract components and structures.

Code Result
Block-Based Coding

Blocks of code:Events block stating, “start on when green flag clicked.Lists block stating, “delete all of data”.Control block stating, “repeat 10”.Inside one nested block.List block stating, “add operator block stating, ”pick random 1 to 35” to data.”Variable block stating, “set max to 1”.Variable block stating, “set min to 35”.Variable block stating, “set position to 1”.Control block stating, “repeat lists block stating, “length of data.””Inside 3 nested blocks. Control block stating, “if item position of data, greater than, max, then”.Inside one nest block.Variables block stating, “set max to variable block stating, “item position of data””.Control block stating, “if item position of data, less than, min, then”.Variables block stating, “set min to variable block stating, “item position of data”Variables blocks stating, “change position by 1”.Variable block stating, “set spread to max, multiplied, min”.

List of 10 set of random value that represents number of one to 35. To the right, 3 windows lift, max of 24, min of 6, and spread of 28.

Spreadsheet

List of data represented in a spreadsheet with random values. The command 'randbetween' between one and 35 is entered 11 times.

List of data represented in a spreadsheet with random values between one and 35.

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 result. Writing code can be similar to writing text. Pseudocode writing, on the other hand, involves writing code directives in the familiar language. Block-based coding can make it easier to follow syntax by using different block shapes and colours.

Here is an example of pseudocode to associate costumes to the result obtained after rolling a die. Here, we use the "if, then, else" command to obtain several possibilities.

Sprite

Costumes: Sprite: 6 dice, they are respectively on their face from one to 6.

Variables: RESULT

SET "RESULT" TO (RANDOM NUMBER 1-6)

IF "RESULT" = 1

THEN PUT ON COSTUME: Sprite with dice costume with face side one.

OR

IF "RESULT" = 2

THEN PUT ON COSTUME: Sprite with dice costume with face side 2.

OR

IF "RESULT" = 3

THEN PUT ON COSTUME: Sprite with dice costume with face side 3.

OR

IF "RESULT" = 4

THEN PUT ON COSTUME: Sprite with dice costume with face side 4.

OR

IF "RESULT" = 5

THEN PUT ON COSTUME: Sprite with dice costume with face side 5.

OR

PUT ON COSTUME: Sprite with dice costume with face side 6.

The resulting code might look like pseudocode, but a good understanding of the use of variables reduces this code to only a few lines.

Blocks of code:Variables block stating, “set result to operator block stating, “pick random 1 to 6”.Looks block stating, “switch costume to variables block stating, “results””.

In this example, all of the "if, then, else" conditions are still there, but exist in the programming of the "switch costume to (RESULT)" block. The student who understands this concept well will be able to dramatically reduce the number of lines of code required to achieve a goal. It is important to distinguish between complexity and length of code, as code can be very complex without being very long.

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.

execution buttons in the Make:Code platform:

Examples of coding execution button.

execution buttons in the Scratch platform

Examples of coding execution button ‘run’.

compile button of the Python compiler "Programiz".

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 screen.

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

Blocks of code:Start on green flag, move right 6 times, move up two times, send message. Start on message, move right 4 times. Start on message, move right 4 times.

Repeating Events

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

Example

Blocks of code:Events block stating, “start on 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 that are placed inside other control structures; for example, loops occurring inside other loops, or a conditional statement being evaluated inside a loop.

Example

Blocks of code:Control block stating, “repeat 3”.Inside 1 nested blocks.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, it’s a square, exclamation mark.”Looks block stating, “say no, coma, it’s a square, exclamation mark.”