Math for Computer Science

Introduction

We learned in an earlier lesson that a variable represents a quantity, or number. When we use a variable in place of a number, we are able to use different numbers at different times. We saw that, for a sister 3 years older than her brother, we could use a variable expression, x − 3, to find the age of the brother, no matter the age of the sister. In this way, variable expressions are quite useful.

In the previous lesson, we learned that mathematics includes multi-step problems. It is also possible to write multi-step variable expressions. For example, consider a boy with an allowance of \$2 per week. If the boy has already saved \$10, we could determine how much money he has all together by first multiplying \$2 by the number of weeks that have passed, then adding the \$10 he has already saved. Thus, we could predict the boy's savings with the expression 2w + 10, where w represents the number of weeks that have passed.

How are variable expressions evaluated?

• To evaluate a variable expression, we first substitute a value for the variable, then apply the order of operations.
• Since we can substitute any value for a variable, variable expressions can be used to predict an outcome for a given condition.

How are variable expressions used in computer science?

In computer programming, variables are often included in expressions that change their value based on user input. In video game development, a variable that represents the player's position could be included in an expression that incorporates user input to change its value and move the player.

Examples

Evaluating variable expressions (one-step)

Q: Evaluate for z = 5:
z + 9

A: First, we substitute for z. Then, we add:
z + 9 = 5 + 9 = 14

Evaluating variable expressions (two-step)

Q: Evaluate for c = 3:
4 × c − 5

A: First, we substitute for c. Then, we apply the order of operations:
4 × c − 5 = 4 × 3 − 5 = 12 − 5 = 7