| English | 의미 |
|---|---|
| nested if/ˈnestɪd ɪf/ | 중첩 if |
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”.
Nesting makes a decision depend on its enclosing path
- A nested if places an if inside another branch. If the inner statement is in the outer true branch, it is reached only when that outer condition is true; an if inside an else is reached on the false path instead.
- Use braces to show that structure. An else without braces associates with the nearest unmatched if, which can differ from what indentation suggests.
A ladder chooses its first matching branch
- An
if ... else if ... elseladder checks conditions from top to bottom until the first true one. With a final else, one branch is selected when condition evaluation completes; without an else, none may run. - The method below accepts ordinary integer scores and returns A, B, C or F. For 85 it checks 90, then 80, returns B, and never evaluates the remaining branch tests.
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";
}
}
}
Distinguish overlap from disjoint cases
- With overlapping lower bounds,
s >= 60includes every score satisfyings >= 90. Putting it first prevents the second branch from being selected, even though Java can compile the ladder. - For pure disjoint tests such as
x < 0,x == 0andx > 0, changing their order while retaining the same outcomes need not change the result. Overlapping policies may instead deliberately prioritize one case; follow the stated requirement.
첫 번째로 참인 분기가 승리합니다
s = 85는 >= 80 분기(B)를 선택하며, 이후 분기는 건너뜁니다.
else-if 계단에서 어떤 조건의 블록이 실행되나요?
난이도 1/5 · 100 코인 + 오늘 첫 완료 시 소액 보너스
Java는 위쪽부터 아래쪽으로 첫 번째 참인 조건을 선택합니다.
if (s>=90) A; else if (s>=80) B; else C; with s = 85일 때, 결과는...
난이도 1/5 · 100 코인 + 오늘 첫 완료 시 소액 보너스
85는 >=90을 만족하지 못하지만 >=80은 만족함 → B, 그리고 종료.
계단에서 광범위한 조건이 좁은 조건보다 앞에 있으면, 좁은 분기는 도달 불가능해질 수 있습니다.
난이도 1/5 · 100 코인 + 오늘 첫 완료 시 소액 보너스
광범위한 조건이 먼저 해당 사례를 잡습니다; 가장 정확한 순서로 배치하십시오.
바깥 if의 참 분기 내부에 배치된 안쪽 if는 바깥 조건이 거짓일 때에도 안쪽 조건이 검사됩니다.
난이도 1/5 · 100 코인 + 오늘 첫 완료 시 소액 보너스
거짓 경로에서는 그 안쪽 문장에 도달하지 못합니다. else 분기 내에 if를 배치했다면包围하는 경로는 달라졌을 것입니다.
s >= 90, s >= 80 및 s >= 70를 사용하는 표시된 등급 사다리에서 의도된 최고 등급을 보존하는 순서는 무엇입니까?
난이도 1/5 · 100 코인 + 오늘 첫 완료 시 소액 보너스
상호 중첩되는 비교를 하기 때문에 더 높은 하한값이 먼저来吧. 이는 모든 부울 조건에 적용되는 보편적인 순서 규칙은 아닙니다.
Separate independent ifs from a ladder
- Two separate statements,
if (s >= 60) ...; if (s >= 90) ...;, both run their bodies for s=95. An else-if ladder links the alternatives and chooses just its first match. - Trace with actual values at the threshold and just below it: 90 goes to A, 89 to B, 80 to B, 79 to C and 69 to F. Testing only 85 would miss a wrong boundary operator.
“First true wins” describes the linked ladder, not multiple independent if statements. A branch can be impossible to select because an earlier comparison catches all its inputs without causing a compile-time unreachable-statement error.
두 개의 독립적 if 문장이 s >= 60과 s >= 90을 테스트합니다. s=95일 때 본문이 실행되는 횟수는 몇 번입니까?
난이도 2/5 · 140 코인 + 오늘 첫 완료 시 소액 보너스
두 조건 모두 참이고 문장들이 독립적이므로 두 본문 모두 실행됩니다.
Trace the false branch of an outer if
- In
if (weekday) { ... } else { if (raining) { ... } }, the inner raining test is reached when weekday is false. The enclosing branch, not the mere word “nested”, determines when it runs. - Identify braces, linked alternatives, overlapping conditions and the actual input before predicting a path. Keep the code's policy and the boundary tests consistent; indentation alone does not determine Java's grouping.
A nested if runs only when control reaches its enclosing branch. A linked ladder selects the first true condition; independent if statements can run several bodies. Higher thresholds come first in this particular grade ladder because its conditions overlap.
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.
바깥 else 분기 안에 있는 if는 바깥 조건이 거짓일 때 실행될 수 있습니다.
난이도 1/5 · 100 코인 + 오늘 첫 완료 시 소액 보너스
예: 바깥의 거짓 경로가 else로 진입하며, 여기서 내부 테스트가 실행될 수 있습니다.