Computer Science Principles
3.3 Math Expressions
3.3 โ Math Expressions
Sequencing, Selection, Iteration, and Arithmetic Operations.
Learning Objectives
- Express an algorithm that uses sequencing without a programming language.
- Represent a step-by-step algorithmic process using sequential code statements.
- Evaluate expressions that use arithmetic operators.
What is an Algorithm?
An algorithm is a finite set of instructions that accomplish a specific task.
Key concepts:
- Sequencing: first step โ second step โ third step
- Selection: decision-making (yes/no) โ different steps depending on outcome
- Iteration: repeat steps until a condition is met
Example Algorithm: Count Even Numbers in a List
- Set
count โ 0 - Get next number from the user
- check if its even, if yes then count โ count + 1
- If more numbers in list, go back to Step 2
- Display
count
Sequencing: Steps 1 โ 2 โ 3 โ 4 โ 5
Selection: Step 3 (even or odd decision)
Iteration: Step 4 (loop until list exhausted)
๐ก Hack 1: Identify Sequencing, Selection, and Iteration
Analyze the following algorithm and identify which parts demonstrate sequencing, selection, and iteration:
- Set
itemto the number to search for - Get next number in the list
- If number =
item, display โitem foundโ - If there are more numbers in the list, go back to Step 2
- Display โitem not foundโ
Code Statements & Expressions
Store values in variables and compute results:
Grade โ 82
highScore โ currentScore
Name โ firstName + lastName
Average โ calcAverage(10, 20, 30)
Hack 2: Predict the Output
Identify what is displayed after running the following code segment:
num1 โ 2
num2 โ 4
num3 โ 5
num1 โ num2 + num3
num3 โ num1 + 5
num2 โ (num1 + num3) / 5
DISPLAY(num1)
DISPLAY(num2)
DISPLAY(num3)
Hints / Thought Process:
- Step 1: Track how
num1,num2, andnum3change at each line. - Step 2: Apply arithmetic operations carefully and in order.
- Step 3: Remember that assignment updates the variable immediately.
Arithmetic Operators & Order of Operations
+ addition (a + b, grade + 10)
- subtraction (a - b, 100 - pointsDeducted)
* multiplication (a * b, base * height)
/ division (a / b, sum / 28)
MOD modulus (remainder) (a MOD b, 17 MOD 2)
note: Python MOD is %
Hack 3: Practice
Predict the value of result after executing the code:
num1 โ 40
num2 โ num1 / 2
num3 โ 5 * num2 + 3
result โ num2 MOD 3 * num1 + 4 - num3 / 2
DISPLAY(result)
๐ฎ Flappy Math โ Expressions
Fly through 10 gates. Each gate asks a tiny math/code question. Answer right to keep going! Click game or press F for fullscreen.
Click / tap to flap. Pass a pipe โ answer a quick expression question. Get to 10!
Tip: please don't use full screen! PRESS SPACE BAR FIRST, THEN CLICK START TO START THE GAME