Math for Computer Science

3 | Variables

Practice Table of Contents

Introduction

Some things never change: the number of inches in a foot; the number of petals on a bluebonnet; the speed of light. Quantities like these are known as constants.

Other things change frequently: the day of the week; the height of a tree; a person’s age. Quantities like these are best represented by variables.

Suppose a sister is three years older than her brother. If we know the sister is twelve years old, we could subtract three years to find the age of her brother. Subtracting three from twelve would work this year, but what about next year? If we don’t know the age of the sister, is it still possible to find the age of her brother?

If we represent the sister’s age with a variable, x, we can write an expression for the age of her brother: x − 3. This expression will always find the age of the brother, no matter how the age of his sister changes.


What are variables?

  • A variable represents a quantity that can change.
  • A constant is a quantity that cannot change.
  • Any symbol can be used as a variable.
  • In computer science, variable quantities are often represented by words or abbreviations to help programmers understand the code.
  • In mathematics, it is customary represent a variable quantity with a letter of the alphabet, like x or y.

How are variables used in computer science?

In a computer program, the same variable name can store many different values, making it possible for a user to achieve varied results. For example, the value of a variable named "score" might change many times during a video game, while a variable named “brightness” might be adjusted by the user of a photo editor.

Examples

Addition and subtraction

Q: Find the value of the variable:
9 + x = 12

A: Since 9 + 3 = 12, the value of x must be 3.


Multiplication and division

Q: Find the value of the variable:
4 × b = 20

A: Since 4 × 5 = 20, the value of b must be 5.

Practice | Variables