Операторы if
Введено| English | Значение |
|---|---|
| if statement/ɪf ˈsteɪtmənt/ | Условный оператор if |
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.
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.

Traffic lights: selection chooses which branch runs, just as if statements choose code paths
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.
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.
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.
Grading with if-else:
if (score >= 90) { grade = "A"; } else { grade = "B"; }score = 95→ conditiontrue→gradeis"A".score = 80→ conditionfalse→gradeis"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-else сколько из двух ветвей выполняются?
Сложность 1/5 · 100 монет + небольшой бонус за первое выполнение сегодня
Точно одна ветвь выполняется в зависимости от условия.
if (score >= 60) выводит Pass, else выводит Fail. При score = 40, что выведется?
Сложность 1/5 · 100 монет + небольшой бонус за первое выполнение сегодня
40 >= 60 — ложь → выполняется блок else → Fail.
Без фигурных скобок оператор if управляет только следующим одиночным оператором.
Сложность 1/5 · 100 монет + небольшой бонус за первое выполнение сегодня
Фигурные скобки группируют блок; без них условным является только один оператор.
Обычный if (без else) просто пропускает свой блок при ложном условии.
Сложность 1/5 · 100 монет + небольшой бонус за первое выполнение сегодня
При отсутствии else ложное условие ничего не делает.
Блок, который выполняется, когда условие if ложно, называется ___ блоком (одно слово).
Сложность 1/5 · 100 монет + небольшой бонус за первое выполнение сегодня
else предоставляет альтернативную ветвь.