Can you do math with variables ? Yes, you can!
Try this:
AGE=6 (press RETURN)
PRINT AGE+1 (press RETURN)
What happened?
The result is 7 because AGE is 6 and 6+1=7.
What happens when you try this:
PRINT AGE+2 (press RETURN)
PRINT AGE+3 (press RETURN)
AGE stays 6, and 6+2=8 and 6+3=9. No surprise here.
You can check that AGE is still 6 by typing this:
PRINT AGE
Press RETURN.
Here is the result:
AGE never changed.
Now try this:
AGE=AGE+1 (press RETURN)
PRINT AGE (press RETURN)
See this screen for the result:
What happened?
Using the = command , AGE was set to AGE+1.
So, AGE is 6. That means AGE+1 is 6+1=7. Using =, you set AGE to 6+1. So, you set AGE to 7. That’s why the result printed is 7.
Let’s try a different example. Type this:
APPLE=5 (press RETURN)
BANANA=2 (press RETURN)
FRUITS=APPLE+BANANA (press RETURN)
PRINT FRUITS (press RETURN)
What happened? Well, APPLE is 5. BANANA is 2. FRUITS is then set to APPLE+BANANA or 5+2. And you know that 5+2=7. Therefore, FRUITS is 7.
Now let’s add pears to the fruits.
PEAR=3 (press RETURN)
FRUITS=FRUITS+PEAR (press RETURN)
PRINT FRUITS (press RETURN)
What do you think the result is?
What happened? PEAR was set to 3. Then FRUITS was set to the number that FRUITS stores (7) plus the number that PEAR stores (3). So, 7+3=10. This is the same as setting FRUITS to APPLE+BANANA+PEAR (5+2+3=10).
Let’s check:
Yay! See what else you can do with variables . Try also +, -, * and /.
Note
Choose your variable names so that the first two letters don’t overlap. BANANA and BARBEQUE are the same variable because the both start with BA. BANANA and BBQ are fine (BA and BB). Also, variable names cannot start with a number or be a command. For example, PRINT=1 will give an error.
Summary
You can use variables to store the result of math. You can also use variables in a math sentence.