Selection: IF · 选择:IF
This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version. · 此页面需较新浏览器(支持 SharedArrayBuffer)。请升级 Chrome、Edge、Firefox 或 Safari 至最新版本。
English
IF … THEN on one line
- Selection chooses what to run.
- The 2026 guide puts
THENon the same line as the condition. - The block ends at
ENDIF.
中文
IF … THEN 写在同一行
- 选择决定运行哪一段。
- 2026 指南把
THEN放在条件的同一行。 - 这一段在
ENDIF结束。
DECLARE Age : INTEGER
Age ← 20
IF Age >= 18 THEN
OUTPUT "You may vote"
ENDIF
English
ELSE is the other path
ELSEruns when the condition isFALSE.- Exactly one of the two blocks runs.
- Nested
IFstatements keep the same shape.
中文
ELSE 是另一条路
- 条件为
FALSE时运行ELSE。 - 两段里恰好运行一段。
- 嵌套的
IF仍是这个形状。
DECLARE Mark : INTEGER
Mark ← 72
IF Mark >= 50 THEN
OUTPUT "Pass"
ELSE
OUTPUT "Fail"
ENDIF
Explore · 探索
TRUE or FALSE — which block runs? · TRUE 还是 FALSE —— 哪个块运行?
IF runs its THEN block when the test is TRUE; ELSE covers every other case. · 测试为 TRUE 时运行 THEN 块;ELSE 处理其余所有情况。
English
Relational operators
=equal,<>not equal,<><=>=.AND,ORandNOTare logic operators. Their operands areBOOLEAN.
中文
关系运算符
=等于,<>不等于,<><=>=。AND、OR和NOT是逻辑运算符。它们的运算数是BOOLEAN。
DECLARE N : INTEGER
N ← 7
IF N MOD 2 = 0 THEN
OUTPUT "even"
ELSE
OUTPUT "odd"
ENDIF
English
AND, OR and NOT
ANDis true only when both tests are true. A mark is in range when it is at least 0 and at most 100.ORis true when either test is true.NOTflipsTRUEandFALSE.- Write the variable on both sides.
Mark >= 0 AND Mark <= 100is the test.Mark >= 0 AND <= 100is not.
中文
AND、OR 与 NOT
- 只有两个判断都为真时,
AND才为真。分数在范围内,是指它不小于 0 且不大于 100。 - 任一判断为真时,
OR为真。NOT把TRUE和FALSE对调。 - 两边都要写上变量。
Mark >= 0 AND Mark <= 100才是这个判断。Mark >= 0 AND <= 100不是。
DECLARE Mark : INTEGER
Mark ← 80
IF Mark >= 0 AND Mark <= 100 THEN
OUTPUT "valid"
ELSE
OUTPUT "invalid"
ENDIF
English
Watch out
THENstays on theIFline. IGCSE 0478 putsTHENon the following line. Do not mix them.- In a test,
=means "is equal to". Storing a value still uses←. "Pass"and"Fail"need quotes.OUTPUT Passlooks up a name called Pass.
中文
注意
THEN留在IF那一行。IGCSE 0478 把THEN放在下一行。不要混用。- 在判断里,
=表示“等于”。存值仍然用←。 "Pass"和"Fail"需要引号。OUTPUT Pass会去查找一个名叫 Pass 的名字。
English
Common mistakes
- Every
IFneeds anENDIF. ELSEbelongs to theIFabove it, not to a nested one, unless you indent it that way.
中文
常见错误
- 每个
IF都要有ENDIF。 ELSE属于它上面的那个IF,除非你把它缩进到嵌套的那一层。
English
Now you try
- Read a number and choose with
IF…THEN…ELSE…ENDIF.
中文
现在你来试
- 读入一个数,用
IF…THEN…ELSE…ENDIF做选择。
Read an INTEGER Mark. Output "Pass" when it is at least 50, otherwise "Fail". · 读入一个 INTEGER Mark。不少于 50 时输出 "Pass",否则输出 "Fail"。
Click Run to see the output here. · 点击“运行”查看此处输出。
Read an INTEGER N. Output positive when it is greater than 0, otherwise not positive. · 读入一个 INTEGER N。大于 0 时输出 positive,否则输出 not positive。
Click Run to see the output here. · 点击“运行”查看此处输出。
Read an INTEGER Mark. Output valid when it is from 0 to · 到 100 inclusive, using AND. Otherwise output invalid. · 读入一个 INTEGER Mark。用 AND 判断它是否在 0 到 100(含两端)内。是则输出 valid,否则输出 invalid。
Click Run to see the output here. · 点击“运行”查看此处输出。