Câu lệnh if
Đã giới thiệu| English | Ý nghĩa |
|---|---|
| if statement/ɪf ˈsteɪtmənt/ | câu lệnh 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.
Nhánh nào sẽ chạy?
Khối của điều kiện đầu tiên đúng sẽ chạy; các khối còn lại bị bỏ qua.
Trong câu lệnh if-else, bao nhiêu nhánh trong hai nhánh sẽ chạy?
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
Chính xác một nhánh chạy dựa trên điều kiện.
if (score >= 60) in Pass, else in Fail. Với score = 40, cái gì sẽ được in ra?
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
40 >= 60 là false → khối else chạy → Fail.
Không có ngoặc nhọn, câu lệnh if chỉ kiểm soát ngay sau nó một câu lệnh duy nhất.
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
Ngōặc nhọn nhóm khối; không có chúng thì chỉ có một câu lệnh mang tính điều kiện.
Một if thuần túy (không có else) đơn giản bỏ qua khối của nó khi điều kiện sai.
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
Không có else, điều kiện sai chỉ không làm gì cả.
Clause chạy khi điều kiện if sai là clause ___ (một từ).
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
else cung cấp nhánh thay thế.