Nested if Statements
導入済み
7:49
選択と反復
ここに3つのループがあります。最初の2つは1文字の違いしかありません。「より小さい」ではなく「以下」です。最初のループは10回実行されます。2番目のループは11回実行されます。…
英語のナレーション・英語と中国語の字幕を切り替え可能
このレッスンの内容
| English | 意味 |
|---|---|
| branch/bræntʃ/ | One possible path of execution selected by a condition. |
| nested if/ˈnestɪd ɪf/ | ネストif |
| condition evaluation | |
| else-if ladder |
このカードは新しいため、翻訳ができるまで英語で表示されます。
A broad first branch hides the top grade
- A grade ladder tests
s >= 60beforeelse if (s >= 90). With s=95, the first condition is true, so its branch 分支 runs and the top-grade branch is never tested. - For these overlapping lower bounds, test the higher threshold first. This is a reasoning rule for this ladder, not a requirement to sort every possible condition from “most specific” to “least specific”.

ネストは決定を外部パスに依存させる
- ネストされたif(nested if) は別の分支内に if を置きます。内部文が外部の真(true)分支にある場合、それは外部の条件が真であるときにのみ到達されます。else 内の if は、False パスで到達されます。
- 構造を示すために波括弧{}を使います。波括弧のない else は、インデントが示唆するものとは異なる、最近の未対応の if に結合します。
階段は最初に一致した分支を選びます
if ... else if ... elseの階段は条件を先頭から順にチェックし、最初に見つかった真のものを選びます。最終的な else がある場合、条件評価完了時に1つの分支が選択されます。else がない場合は、いずれも実行されない可能性があります。- 以下のメソッドは通常の整数スコアを受け取り、A, B, C, F のいずれかを返します。85 の場合、90 をチェックし、次に 80 をチェックして B を返すため、残りの分支テストは評価されません。
public class GradeChoice {
public static String grade(int s) {
if (s >= 90) {
return "A";
} else if (s >= 80) {
return "B";
} else if (s >= 70) {
return "C";
} else {
return "F";
}
}
}
重複と非重複の場合を区別する
- 重なる下限値がある場合、
s >= 60はs >= 90を満たすすべてのスコアを含む。これを最初に配置すると、Javaがladderをコンパイルできるにもかかわらず、2番目の分岐は選択されない。 x < 0,x == 0,x > 0のような純粋な非重複テストの場合、結果を変えずに順序を変更しても、結果が変わらない場合があります。重複するポリシーはむしろ意図的にあるケースを優先することがありますので、明記された要件に従ってください。
The first true branch wins
s = 85 takes the >= 80 branch (B); later branches are skipped.
In an else-if ladder, which condition's block runs?
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
Java takes the first true condition top to bottom.
For if (s>=90) A; else if (s>=80) B; else C; with s = 85, the result is...
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
85 fails >=90 but passes >=80 → B, then stop.
If a broad condition comes before a narrower one in a ladder, the narrower branch can become unreachable.
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
The broad condition catches those cases first; put tightest first.
For an inner if placed inside the outer if's true branch, the inner condition is checked even when the outer condition is false.
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
That inner statement is not reached on the false path. An if placed inside an else branch would have a different enclosing path.
For the shown grade ladder using s >= 90, s >= 80 and s >= 70, which ordering preserves the intended top grades?
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
Higher lower bounds come first because these comparisons overlap. This is not a universal ordering rule for all Boolean conditions.
ラダーから独立したif文を分離する
- 2つの独立した
if (s >= 60) ...; if (s >= 90) ...;文は、s=95のときともに本体を実行します。else-ifラダーは代替案を連結し、最初の一致のみを選択します。 - 閾値およびその直下の値でトレースします:90はAへ、89はBへ、80はBへ、79はCへ、69はFへと分岐します。85のみを試すだけでは、誤った境界演算子を見落とすことになります。
「最初のtrueが勝つ」は連結されたラダーを指し、複数の独立したif文ではありません。ある分支が選択できない場合があります。これは、より前の比較がコンパイル時の未到達コードエラーを起こさずにその分支の入力すべてを捕捉してしまうためです。
Two independent if statements test s >= 60 and s >= 90. For s=95, how many bodies run?
難易度 2/5 · 140 コイン + 今日初回達成時の小ボーナス
Both conditions are true and the statements are independent, so both bodies run.
外側のifのfalse分支をトレースする
if (weekday) { ... } else { if (raining) { ... } }において、内側のrainingテストはweekdayがfalseのときに到達します。実行される時期を決定するのは単に「ネスト」という言葉ではなく、それを包む分支です。- 曲げ括弧、連結された代替案、重なる条件、そして実際の入力を確認してからパスを予測してください。コードのポリシーと境界テストを整合させます。インデントだけでJavaのグループ分けが決まるわけではありません。
ネストされたifは制御が包む分支に到達したときにのみ実行されます。連結されたラダーは最初のtrue条件を選択し、独立したif文は複数の本体を実行することがあります。この特定の成績ラダーでは、条件が重なり合うため、高い閾値が最初に来ます。
With if (s >= 60) result = 1; else if (s >= 90) result = 2; else result = 3;, what is result for s = 95?
難易度 2/5 · 140 コイン + 今日初回達成時の小ボーナス
95 satisfies the first comparison, so result becomes 1 and the else-if branch is skipped.
An if inside an outer else branch can be reached when the outer condition is false.
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
Yes: the outer false path enters else, where the inner test may run.
何が問題でしたか?この内容はサイトの運営チームだけが確認します。