Variables and DECLARE
This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version.
A variable stores a value
- A variable holds a value you can use and change.
- The arrow
←puts a value into a variable (this is assignment). - On your keyboard, type
<-(less-than, minus) — it means the same as←.
Age ← 16
OUTPUT Age // → 16
DECLARE names the type
DECLAREstates a variable's name and its data type.- It is good style to declare a variable before you use it.
DECLARE Count : INTEGER
Count ← 0
OUTPUT Count // → 0
You can change a variable
- Assign again to give a variable a new value.
- The right-hand side is worked out first, then stored.
Total ← 10
Total ← Total + 5
OUTPUT Total // → 15
Explore
Watch the value change
← puts a value into a box. Assign again, and the box holds the new value.
Watch out
←(or<-) means store into. It is not the=used to test equality.- A variable keeps its last value until you assign a new one.
Common mistakes
DECLARE name : TYPEbefore you use a variable.- Assign with
←(the arrow).
Now you try
- Type
<-for the←arrow. - Press Run to see the output, then Check answer.
Put 16 into a variable called Age, then OUTPUT it. (Type <- for the ← arrow.)
Click Run to see the output here.
DECLARE a variable Price as INTEGER, set it to 3, then OUTPUT Price * 4 (the answer is 12).
Click Run to see the output here.
Set Total to 10, then add 5 to it, then OUTPUT Total (the answer is 15).
Click Run to see the output here.