For out purposes, variables in EZ Math Movie work a lot like they do in any math discussion. Here we will confine ourselves to thinking about variables as symbols for values, or as containers for values. Imagine putting numbers into them. Here is a simple statement that assigns a value to a variable:
x = 5;
The name of the variable is 'x'. The equal sign could be read: 'is set equal to'. And '5' is the value which is 'placed inside' of x, or which x 'holds', or to which x is 'set'.
Note the semi-colon at the end of the program line.
In many math subjects variable names are only one letter long. For example, we might use h for the height of a box, as in:
h = 8;
But in EZ Math Movie variables can be whole words or phrases, as in these examples:
height = 8; boxHeight = 8; theHeightOfTheBox = 8;
Digits can appear within a variable name, but not as the first character, as in:
h2 = 8; height3 = 8; box5Height = 8;
We will adopt the following naming conventions in EZ Math Movie:
We will usually start the spelling of a variable with a lower case letter. If the variable name is a phrase a few words long, we will capitalize the start of each word within the variable name, as in:
thisIsAReallyLongVariableName = 3;
This style of writing variable names is called 'camel case'.
Lets take a look at how to add, subtract, multiply, and divide.
Addition: Uses plus sign, as in 'a equals b plus 3':
a = b + 3;
Subtraction: Uses the minus sign, as in 'c equals 4 minus d':
c = 4 - d;
Multiplication: Uses the asterisk, as in 'e equals f times 5':
e = f * 5;
Division: Use the forward slash, as in 'g equals 6 divided by h':
g = 6 / h;
A math expression can contain more than one operation. It could have both an addition and multiplication:
i = 2 + 3 * 5;
What happens first, the addition or multiplication? That is a question about math operation precedence. The rules are:
In the above example 3 times 5 would happen first, evaluating to 15, then the 2 would be added. The variable i would be set to 17.
If we wanted 2 and 3 to be added first, we would need to use parentheses:
i = (2 + 3) * 5;
The variable i would be equal to 25, since 2 plus 3 is 5, and 5 times 5 is 25.
The example program demonstrates how to use a variable; and how to add, subtract, multiply, and divide.
Next tutorial: Random numbers
Here are some links to related articles at EZ Programing Demos: