Variables, DECLARE and CONSTANT · 变量、DECLARE 与 CONSTANT
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←.
变量存放一个值
- 变量(variable)存放一个你可以使用和修改的值。
- 箭头
←把一个值放进变量(这就是赋值)。 - 在键盘上,输入
<-(小于号、减号)—— 它和←意思相同。
Age ← 16
OUTPUT Age // → 16
OUTPUT "Age" // → Age
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 声明类型
DECLARE说明变量的名字和它的数据类型。- 在使用变量之前先声明它,是好的风格。
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
Watch the value change · 看着值发生变化
← puts a value into a box. Assign again, and the box holds the new value. · ← 把一个值放进盒子。再次赋值,盒子就装上新值。
A constant does not change
- A constant is a name for a value the program must not change, such as a pass mark or an hourly rate.
- The 2026–2028 syllabus writes
CONSTANT HourlyRate ← 6.50— the arrow, then a literal only. - If the number changes next year, you edit that one line and every use of the name follows.
常量不能被改变
- 常量(constant)是一个程序不许修改的值的名字,例如及格分或每小时的单价。
- 2026–2028 考纲写成
CONSTANT HourlyRate ← 6.50—— 箭头,右边只能是一个字面量。 - 明年数字变了,只改这一行,所有用到这个名字的地方都会跟着变。
CONSTANT PassMark ← 50
OUTPUT PassMark // → 50
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.
OUTPUT Ageprints the number in the box.OUTPUT "Age"prints the word Age.
注意
←(或<-)表示存进去。它不是用来判断相等的=。- 变量会一直保留它上一次的值,直到你给它赋一个新值。
OUTPUT Age打印盒子里的数字。OUTPUT "Age"打印单词 Age。
Common mistakes
DECLARE <identifier> : <data type>before you use a variable.- A constant is
CONSTANT <identifier> ← <value>. Assign a variable with←.
常见错误
- 使用变量前要
DECLARE <identifier> : <data type>。 - 常量写成
CONSTANT <identifier> ← <value>。变量用←赋值。
Now you try
- Type
<-for the←arrow. - Press Run to see the output, then Check answer.
现在轮到你
- 用
<-表示←箭头。 - 按 Run 看输出,然后按检查答案。
Put 16 into a variable called Age, then OUTPUT it. (Type <- for the ← arrow.) · 把 16 放进一个名为 Age 的变量,然后 OUTPUT 它。(用 <- 表示 ← 箭头。)
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). · 把变量 Price 用 DECLARE 声明为 INTEGER,设为 3,然后 OUTPUT Price * 4(答案是 12)。
Click Run to see the output here. · 点击“运行”查看此处输出。
Set · 集合 Total to · 到 10, then add 5 to it, then OUTPUT Total (the answer is 15). · 把 Total 设为 10,再给它加 5,然后 OUTPUT Total(答案是 15)。
Click Run to see the output here. · 点击“运行”查看此处输出。
Declare a constant PassMark with the value 50, using ←, then OUTPUT it. · 用 ← 声明常量 PassMark,值为 50,然后 OUTPUT 它。
Click Run to see the output here. · 点击“运行”查看此处输出。