© Gerald Friedland 2019
Gerald FriedlandBeginning Programming Using Retro Computinghttps://doi.org/10.1007/978-1-4842-4146-2_9

9. Math and Variables

Gerald Friedland1 
(1)
Berkeley, CA, USA
 

Can you do math with variables ? Yes, you can!

Try this:

AGE=6 (press RETURN)

PRINT AGE+1 (press RETURN)

../images/470770_1_En_9_Chapter/470770_1_En_9_Figa_HTML.jpg

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:

../images/470770_1_En_9_Chapter/470770_1_En_9_Figb_HTML.jpg

AGE never changed.

Now try this:

AGE=AGE+1 (press RETURN)

PRINT AGE (press RETURN)

See this screen for the result:

../images/470770_1_En_9_Chapter/470770_1_En_9_Figc_HTML.jpg

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)

../images/470770_1_En_9_Chapter/470770_1_En_9_Figd_HTML.jpg

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?

../images/470770_1_En_9_Chapter/470770_1_En_9_Fige_HTML.jpg

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:

../images/470770_1_En_9_Chapter/470770_1_En_9_Figf_HTML.jpg

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.137.240