使用 if/else 做选择
根据条件分支
使用 if/else 做选择 是 CoddyKit 上的免费 Mojo Academy 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Mojo Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Mojo Academy 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Code That Decides
Programs often need to choose a path. The if statement runs a block only when its condition is true, so your code can react to data. 🔀
Your First if
Write if, a condition, a colon, then an indented block. If the condition holds, Mojo runs the block; otherwise it skips it entirely.
if score > 90:
print("Top marks!")Conditions Are Booleans
The bit after if must boil down to True or False. Comparisons like greater-than and equals naturally produce a boolean for you.
var ready = count == 0
if ready:
print("Empty")Add an else Branch
Pair else with if to cover the other case. Exactly one of the two blocks runs, so nothing is ever left unhandled.
if temp > 30:
print("Hot")
else:
print("Mild")Indentation Defines the Block
Like Python, Mojo uses indentation to mark what belongs to a branch. Consistent spaces, usually four, keep the structure clear and valid.
Comparison Operators
Build conditions with operators like ==, !=, <, and >=. Each compares two values and hands back the boolean that if needs.
if age >= 18:
print("Adult")Chain More Cases with elif
Need more than two paths? Insert one or more elif branches between if and else to test extra conditions in order.
if g >= 90:
print("A")
elif g >= 80:
print("B")
else:
print("C")Only the First Match Wins
Mojo checks branches top to bottom and stops at the first condition that is true. The rest are skipped, even if they would also match.
Combine Conditions with and / or
Join checks using and and or. With and both must be true; with or just one is enough to enter the block.
if hungry and has_food:
print("Eat now")Flip a Condition with not
Put not in front of a condition to invert it. It turns True into False, handy for checking the absence of something.
if not logged_in:
print("Please sign in")Nesting Branches
You can place an if inside another branch to refine a decision. Keep nesting shallow so the logic stays easy to read.
if signed_in:
if is_admin:
print("Dashboard")Quick Check
You want to handle three or more separate cases in one decision. Which keyword do you reach for?
Recap
You branch with if, add else for the other case, and chain elif for more. Conditions are booleans you can combine with and, or, and not. 🎯
常见问题解答
「使用 if/else 做选择」课时是免费的吗?
是的 — 「使用 if/else 做选择」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Mojo Academy 课程的其余内容,请升级到 CoddyKit PRO。 Mojo Academy 课程共包含 4 节课。
「使用 if/else 做选择」这节课中我会学到什么?
根据条件分支 你通过在浏览器中直接运行的动手代码来练习 Mojo Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Mojo Academy 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Mojo Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。
「使用 if/else 做选择」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Mojo Academy 课中编写并运行代码吗?
能。每节 Mojo Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 使用 if/else 做选择
- 使用 while 循环
- 使用 for 和 range 迭代
- break、continue 与提前退出