Selection: CASE
Cambridge Pseudocode Lesson 6 1:59 English narration · English + 中文 subtitles burned in
Chapters
Transcript
When one variable is checked against several fixed values, a stack of IFs gets long.
当一个变量要跟好几个固定的值比对时,一叠 IF 会写得很长。
CASE says the same thing in one block.
CASE 用一个块说同样的事。
Grade holds two.
Grade 里是 2。
CASE compares it with one — no — then two matches, so that branch runs, and it stops there.
CASE 先拿它和 1 比——不是——然后和 2 匹配上了, 于是那条分支运行,并且到此为止。
Silver is printed and the rest of the list is never looked at.
Silver 被打印出来,列表剩下的部分根本不会被看一眼。
The written shape has three parts and the exam wants all three.
写出来的形状有三个部分,考卷三个都要。
The block opens with CASE OF, then the variable being tested.
这一块以 CASE OF 开始,后面跟着被测试的那个变量。
Each branch is a value, a colon, and what to do — and the colon is not optional, it is how the marker knows where the value stops.
每一条分支是一个值、一个冒号,再加上要做的事—— 而那个冒号不是可有可无的,它是判卷人知道值到哪里结束的依据。
Then ENDCASE closes the whole thing, exactly as ENDIF did.
然后 ENDCASE 把整个东西关上,就像 ENDIF 一样。
A list of values can never cover everything, so OTHERWISE is the catch-all: it runs when nothing above it matched.
一串值永远不可能覆盖所有情况,所以 OTHERWISE 是那个兜底的: 当它上面的都没匹配上时,它就运行。
Day holds six, neither branch matches, and OTHERWISE answers.
Day 里是 6,两条分支都不匹配,于是 OTHERWISE 作答。
One placement rule — it goes last, just before ENDCASE.
一条位置规则——它放在最后,紧挨着 ENDCASE 前面。
Put it higher up and it swallows every branch beneath it.
放高了,它就会把它下面的每一条分支都吞掉。
One limit, and it decides which structure to reach for.
有一个限制,而它决定了你该用哪一种结构。
A CASE branch is a VALUE, not a test, so you cannot write a range in it.
CASE 的一条分支是一个"值",不是一个"测试", 所以你不能在里面写一个范围。
Anything with a greater than or a less than in it — that is an IF.
任何带大于号或小于号的东西——那是 IF 的活。
Use CASE when one variable is checked against a few fixed values, and an IF for everything else.
当一个变量要跟几个固定的值比对时用 CASE,其余的一律用 IF。
Four things to take with you.
带走四点。
One: CASE OF compares one variable against set values.
第一:CASE OF 把一个变量和一组固定的值比对。
Two: each branch is a value, colon, result.
第二:每条分支是"值、冒号、结果"。
Three: OTHERWISE goes last and catches the rest.
第三:OTHERWISE 放在最后,兜住其余情况。
Four: for a range like Mark greater than or equal to fifty, use an IF.
第四:遇到像"Mark 大于等于 50"这样的范围,用 IF。
Now do the three tasks.
现在去做那三道题。