if Statements · if 语句
| English | 中文 | Pinyin · 拼音 |
|---|---|---|
| if statement/ɪf ˈsteɪtmənt/ | 条件语句 | tiáo jiàn yǔ jù |
Running code conditionally
- An if statement 条件语句 runs a block only when its condition is
true. if (score >= 60) { System.out.println("Pass"); }- If the condition is
false, the block is skipped entirely. - This is selection — the program chooses whether to run the block.
有条件地运行代码
- if 语句(条件语句)只在条件为
true时运行一个块。 if (score >= 60) { System.out.println("Pass"); }- 如果条件为
false,这个块被完全跳过。 - 这就是选择——程序挑选是否运行这个块。
Adding else
- An
elseclause runs alternative code when the condition isfalse. if (score >= 60) { print("Pass"); } else { print("Fail"); }- Exactly one of the two branches runs — never both, never neither.
elseis optional; a bareifjust skips its block when false.
加上 else
else子句在条件为false时运行替代代码。if (score >= 60) { print("Pass"); } else { print("Fail"); }- 两个分支中恰好一个运行——绝不两个都运行,也绝不都不运行。
else是可选的;光有if在为假时只是跳过它的块。
Braces group the block
- Braces
{ }group the statements that belong to theiforelse. - Without braces, only the single next statement is controlled by the
if. - Always use braces, even for one line — it prevents a common bug.
- Indent the block so the structure is visible at a glance.
花括号把块组合起来
- 花括号
{ }把属于if或else的语句组合起来。 - 没有花括号,只有紧接着的单条语句受
if控制。 - 永远用花括号,即使只有一行——它能防止一个常见错误。
- 缩进这个块,让结构一眼可见。
Tracing the branch
- To trace an
if-else, evaluate the condition, then run the matching branch. - With
score = 75:75 >= 60istrue→ run theifblock → prints "Pass". - With
score = 40:40 >= 60isfalse→ run theelseblock → prints "Fail". - Only the code in the chosen branch executes.
追踪分支
- 要追踪
if-else,先求条件,再运行匹配的分支。 - 当
score = 75:75 >= 60为true→ 运行if块 → 打印 "Pass"。 - 当
score = 40:40 >= 60为false→ 运行else块 → 打印 "Fail"。 - 只有被选中分支里的代码会执行。
Without braces, an if controls only the ONE statement right after it. So if (x > 0) a(); b(); always runs b() — the indentation lies. Always wrap the block in { }. And in an if-else, exactly one branch runs; you never fall through into both, unlike separate if statements.
**没有花括号,if 只控制它后面的那一条语句。**所以 if (x > 0) a(); b(); 总会运行 b()——缩进在撒谎。永远用 { } 把块包起来。而在 if-else 里,恰好一个分支运行;你绝不会像分开的 if 语句那样两个都进入。
Grading with if-else:
if (score >= 90) { grade = "A"; } else { grade = "B"; }score = 95→ conditiontrue→gradeis"A".score = 80→ conditionfalse→gradeis"B".
用 if-else 评分:
if (score >= 90) { grade = "A"; } else { grade = "B"; }score = 95→ 条件true→grade是"A"。score = 80→ 条件false→grade是"B"。
An if statement runs a block only when its condition is true; an optional else runs alternative code when it's false — exactly one branch executes. Use braces { } to group each block (without them, only the next single statement is controlled). Trace by evaluating the condition, then running the matching branch.
if 语句只在条件为 true 时运行一个块;可选的 else 在为 false 时运行替代代码——恰好一个分支执行。用花括号 { } 组合每个块(没有它们,只有下一条单语句受控)。追踪方法:先求条件,再运行匹配的分支。
Which branch runs? · 哪个分支运行?
The first true condition's block runs; the rest are skipped. · 第一个为真条件的块运行;其余被跳过。
In an if-else statement, how many of the two branches run? · 在 if-else 语句中,两个分支中有几个运行?
Exactly one branch runs based on the condition. · 根据条件,恰好一个分支运行。
if (score >= 60) prints Pass, else prints Fail. With score = 40, what prints? · if (score >= 60) 打印 Pass,else 打印 Fail。当 score = 40 时打印什么?
40 >= 60 is false → the else block runs → Fail. · 40 >= 60 为假 → else 块运行 → Fail。
Without braces, an if statement controls only the single next statement. · 没有花括号,if 语句只控制紧接着的单条语句。
Braces group the block; without them only one statement is conditional. · 花括号组合块;没有它们只有一条语句是有条件的。
A bare if (no else) simply skips its block when the condition is false. · 光有 if(没有 else)在条件为假时只是跳过它的块。
With no else, a false condition just does nothing. · 没有 else 时,假条件什么也不做。
The clause that runs when an if condition is false is the ___ clause (one word). · 在 if 条件为假时运行的子句是 ___ 子句(填英文一词 else)。
else provides the alternative branch. · else 提供替代分支。