Nested decisions · 嵌套选择
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
One mark, three outcomes
- A mark of 80 or more is a distinction, 50 or more is a pass, and anything lower is a fail.
- One
IFhas two paths. The second decision goes inside theELSE, as its ownIF. - Each
IFhas its ownENDIF. Indent the inner one so you can see whichELSEit belongs to.
中文
一个分数,三种结果
- 80 分及以上是优秀,50 分及以上是及格,再低就是不及格。
- 一个
IF有两条路。第二个判断放在ELSE里面,写成它自己的IF。 - 每个
IF都有自己的ENDIF。把内层缩进,才能看出ELSE属于谁。
DECLARE Mark : INTEGER
Mark ← 72
IF Mark >= 80
THEN
OUTPUT "Distinction"
ELSE
IF Mark >= 50
THEN
OUTPUT "Pass"
ELSE
OUTPUT "Fail"
ENDIF
ENDIF
English
Count the marks that count
- A validation tally walks the list and adds 1 only when the value is in range.
- The range test is the
ANDfrom the previous lesson, inside the loop.
中文
数一数范围内的分数
- 校验计数沿着列表走,只有值在范围内才加 1。
- 范围判断就是上一课的
AND,放在循环里面。
DECLARE Mark : ARRAY[1:4] OF INTEGER
DECLARE I, Count : INTEGER
Mark[1] ← 80
Mark[2] ← -1
Mark[3] ← 100
Mark[4] ← 40
Count ← 0
FOR I ← 1 TO 4
IF Mark[I] >= 0 AND Mark[I] <= 100
THEN
Count ← Count + 1
ENDIF
NEXT I
OUTPUT Count // → 3
English
Watch out
- The inner
IFstarts on the line afterELSE. It is not written on theELSEline. - Close the inner
ENDIFbefore the outer one. The lastENDIFbelongs to the firstIF.
中文
注意
- 内层
IF从ELSE的下一行开始。不要把它写在ELSE那一行。 - 先写下内层的
ENDIF,再写外层的。最后一个ENDIF属于第一个IF。
English
Common mistakes
- An
ENDIFin the wrong place closes the outer test and leaves the inner one open. - A mark of exactly 50 is a pass, and exactly 80 is a distinction. The tests use
>=.
中文
常见错误
ENDIF放错位置会先关掉外层判断,内层就没有被关上。- 正好 50 分是及格,正好 80 分是优秀。判断用
>=。
English
Now you try
- The first two tasks are nested decisions. The third counts with
AND. - Press Check answer to test it.
中文
现在你来试
- 前两题是嵌套判断。第三题用
AND计数。 - 按检查答案来测试。
Read an INTEGER Mark. Output Distinction for 80 or more, otherwise Pass for 50 or more, otherwise Fail. Nest the second IF under · 位于 ELSE. · 读入一个 INTEGER Mark。80 及以上输出 Distinction,否则 50 及以上输出 Pass,否则输出 Fail。把第二个 IF 放在 ELSE 下面。
Click Run to see the output here. · 点击“运行”查看此处输出。
Same three outcomes as the previous task. This input should take the inner ELSE. · 结果仍是上一题的三种。这一次的输入应该走进内层的 ELSE。
Click Run to see the output here. · 点击“运行”查看此处输出。
The array Mark holds · 持有 80, -1, 100, 40. Count how many values are from 0 to · 到 100 inclusive and output the count (it is 3). · 数组 Mark 里是 80、-1、100、40。数一数有多少个值在 0 到 100(含两端)内,并输出个数(是 3)。
Click Run to see the output here. · 点击“运行”查看此处输出。