C3. Coding
Specific Expectations
Coding Skills
C3.1
solve problems and create computational representations of mathematical situations by writing and executing code, including code that involves conditional statements and other control structures
- mathematical situations involving conditional statements:
- comparing budgets to actual spending
- comparing perimeters or areas of shapes
- running probability simulations
- classifying angles
- solving inequalities, such as code that would determine who can ride the roller coaster at an amusement park:
- determining the minimum and maximum values from a set of numbers
- programming a “Guess that Number” game:
- programming a rock, paper, scissors game
- code involving conditional statements:
“If then” statements:
“If then else” statements:
Note:
- Assignment statements assign a value to a variable and usually use a single equal sign (=), while comparison statements are used to compare two values and usually use a double equal sign (==) for equal to, < for less than, > for greater than, <= for less than or equal to, and >= for greater than or equal to.
- Pseudocode does not represent a specific programming language. It can be adapted to work with a variety of programming languages and/or environments.
- other control structures:
- Conditional statements are a representation of binary logic (yes or no, true or false, 1 or 0).
- A conditional statement evaluates a Boolean condition, something that can be either true or false.
- Conditional statements are usually implemented as “if…then” statements or “if…then…else” statements. If a conditional statement is true, then there is an interruption in the current flow of the program being executed and a new direction is taken or the program will end.
- Conditional statements, like loops, can be nested to allow for a range of possible outcomes or to implement decision trees.
Note
- Coding can support the development of a deeper understanding of mathematical concepts.
- Coding can be used to learn how to automate simple processes and enhance mathematical thinking. For example, students can code expressions to recall previously stored information (defined variables), then input values (e.g., from a sensor, count, or user input) and redefine the value of the variable. For examples of these, refer to the notes in SEs C2.1, C2.2, and C2.3.
- The construction of the code should become increasingly complex and align with other developmentally appropriate learning.
Have students write code to find the area of a rectangle. To set up the task, discuss what variables will be important to include in the code: What information do you need to determine the area of a rectangle? How do you know?
- base
- height
- area = base × height
Next, have students develop a program that receives input (dimensions of the rectangle) from a user and provides the area of a rectangle as output.
Logic | Pseudocode |
Ask the user. | What is the base of your rectangle? |
Store user input. | base = user input |
Ask the user. | What is the height of your rectangle? |
Store user input. | height = user input |
Calculate. | area = base * height |
Output. | The area of your rectangle is area units. |
Next, have students adapt their code so that it allows the user to compare the area of two different rectangles.
Logic | Pseudocode |
Ask the user. | What is the base of rectangle A? |
Store user input. | baseA = user input |
Ask the user. | What is the height of rectangle A? |
Store user input. | heightA = user input |
Calculate and store. | areaA = baseA * heightA |
Ask the user. | What is the base of rectangle B? |
Store user input. | baseB = user input |
Ask the user. | What is the height of rectangle B? |
Store user input. | heightB = user input |
Calculate and store. | areaB = base * heightB |
Compare. | if areaA = = areaB |
Output | Rectangle A has the same area as Rectangle B. |
Else Comparison | else if areaA > areaB |
Output | Rectangle A’s area is greater than Rectangle B’s area. |
Else | else |
Output | Rectangle A’s area is less than Rectangle B’s area. |
Note:
- Pseudocode does not represent a specific programming language. It can be adapted to work with a variety of programming languages and/or environments.
- Assignment statements usually use a single equal sign (=), and comparison statements usually use a double equal sign (==).
C3.2
read and alter existing code, including code that involves conditional statements and other control structures, and describe how changes to the code affect the outcomes
- situations that involve altering code:
- enhancing mathematical learning
- simplifying code
- reinforcing that there is more than one way to generate a given outcome
- debugging code to get the desired outcome
- remixing (altering) a program and using it for another purpose
- Reading code is done to make a prediction about what the expected outcome will be. Based on that prediction, one can determine if the code needs to be altered prior to its execution.
- Reading code helps with troubleshooting why a program is not able to execute.
- Code must sometimes be altered so that the expected outcome can be achieved.
- Code can be altered to be used for a new situation.
Note
- When students are reading code, they are exercising problem-solving skills related to predicting and estimating.
- When code is altered with the aim of reaching an expected outcome, students get instant feedback when it is executed. Students exercise problem-solving strategies to further alter the program if they did not get the expected outcome. If the outcome is as expected, but it gives the wrong answer mathematically, students will need to alter their thinking.
Provide students with the code shown below, which creates a numerical growing pattern through 10 repeats of adding 2.5. Have them alter the code so that it generates a different numerical growing pattern for outputs of less than 50:
Ask students to modify their code from C3.1: Sample Task 1 so that the code compares the areas of two triangles. For example:
Logic | Pseudocode |
Ask the user. | What is the base of triangle A? |
Store user input. | baseA = user input |
Ask the user. | What is the height of triangle A? |
Store user input. | heightA = user input |
Calculate and store. | areaA = baseA * heightA/2 |
Ask the user. | What is the base of triangle B? |
Store user input. | baseB = user input |
Ask the user. | What is the height of triangle B? |
Store user input. | heightB = user input |
Calculate and store. | areaB = baseB * heightB/2 |
Compare. | if areaA = = areaB |
Output | Triangle A has the same area as Triangle B. |
Else Comparison | else if areaA > areaB |
Output | Triangle A’s area is greater than Triangle B’s area. |
Else | else |
Output | Triangle A’s area is less than Triangle B’s area. |