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:
![](/img/activite/algebre/en/7e/VE7_Algebre_Image144_en.png)
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.
![](/img/activite/algebre/en/7e/VE7_Algebre_Image145_en.png)
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.”](/img/activite/algebre/en/7e/VE7_Algebre_Image146_en.png)
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.](/img/activite/algebre/en/7e/VE7_Algebre_Image147_en.png)
Run buttons in MakeCode
![Examples of coding execution button.](/img/activite/algebre/en/7e/VE7_Algebre_Image148_en.png)
Run buttons in Scratch
![Examples of coding execution button ‘run’.](/img/activite/algebre/en/7e/VE7_Algebre_Image149_en.png)
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.
![](/img/activite/algebre/en/7e/VE7_Algebre_Image150_en.png)
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.”](/img/activite/algebre/en/7e/VE7_Algebre_Image151_en.png)
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.](/img/activite/algebre/en/7e/VE7_Algebre_Image165_en.png)
Concurrent Events
Several events that occur at the same time.
Example
![](/img/activite/algebre/en/7e/VE7_Algebre_Image164_1_en.png)
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”.](/img/activite/algebre/en/7e/VE7_Algebre_Image163_en.png)
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”.](/img/activite/algebre/en/7e/VE7_Algebre_Image162_en.png)
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.”](/img/activite/algebre/en/7e/VE7_Algebre_Image161_en.png)
Code Efficiency
Writing functional code using a minimum of blocks or commands.
Example
![](/img/activite/algebre/en/7e/VE7_Algebre_Image160_en.png)