if Statements · Instruções if
| English | Português |
|---|---|
| if statement/ɪf ˈsteɪtmənt/ | instrução 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.
Executando código condicionalmente
- Um if statement 条件语句 executa um bloco apenas quando sua condição é
true. if (score >= 60) { System.out.println("Pass"); }- Se a condição for
false, o bloco é pulado inteiramente. - Isso é seleção — o programa escolhe se executa o bloco ou não.
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.
Adicionando else
- Uma cláusula
elseexecuta código alternativo quando a condição éfalse. if (score >= 60) { print("Pass"); } else { print("Fail"); }- Exatamente um dos dois ramos executa — nunca ambos, nunca nenhum.
elseé opcional; umifsem else apenas pula seu bloco quando falso.
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.
Chaves agrupam o bloco
- Chaves
{ }agrupam as instruções pertencentes aoifouelse. - Sem chaves, apenas a próxima instrução única é controlada pelo
if. - Sempre use chaves, mesmo para uma linha — isso evita um bug comum.
- Indente o bloco para que a estrutura seja visível à primeira vista.
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.
Rastreando o ramo
- Para rastrear um
if-else, avalie a condição, depois execute o ramo correspondente. - Com
score = 75:75 >= 60étrue→ execute o blocoif→ imprime "Pass". - Com
score = 40:40 >= 60éfalse→ execute o blocoelse→ imprime "Fail". - Apenas o código no ramo escolhido é executado.
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.
Sem chaves, um if controla apenas a ÚNICA instrução logo após ele. Então if (x > 0) a(); b(); sempre executa b() — a indentação engana. Sempre envolva o bloco em { }. E em um if-else, exatamente um ramo executa; você nunca cai em ambos, diferentemente de statements if separados.
Grading with if-else:
if (score >= 90) { grade = "A"; } else { grade = "B"; }score = 95→ conditiontrue→gradeis"A".score = 80→ conditionfalse→gradeis"B".
Avaliando com if-else:
if (score >= 90) { grade = "A"; } else { grade = "B"; }score = 95→ condiçãotrue→gradeé"A".score = 80→ condiçãofalse→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.
Um if statement executa um bloco apenas quando sua condição é true; um else opcional executa código alternativo quando ela é false — exatamente um ramo executa. Use chaves { } para agrupar cada bloco (sem elas, apenas a próxima instrução única é controlada). Rastree avaliando a condição, depois executando o ramo correspondente.
Which branch runs? · Qual ramo executa?
The first true condition's block runs; the rest are skipped. · O bloco da primeira condição true executa; os demais são pulados.
In an if-else statement, how many of the two branches run? · Em uma instrução if-else, quantos dos dois ramos executam?
Exactly one branch runs based on the condition. · Exatamente um ramo executa baseado na condição.
if (score >= 60) prints Pass, else prints Fail. With score = 40, what prints? · if (score >= 60) imprime Pass, else imprime Fail. Com score = 40, o que é impresso?
40 >= 60 is false → the else block runs → Fail. · 40 >= 60 é false → o bloco else executa → Fail.
Without braces, an if statement controls only the single next statement. · Sem chaves, uma instrução if controla apenas a próxima única declaração.
Braces group the block; without them only one statement is conditional. · Chaves agrupam o bloco; sem elas, apenas uma declaração é condicional.
A bare if (no else) simply skips its block when the condition is false. · Um if nu (sem else) simplesmente pula seu bloco quando a condição é false.
With no else, a false condition just does nothing. · Sem else, uma condição falsa simplesmente não faz nada.
The clause that runs when an if condition is false is the ___ clause (one word). · A cláusula que executa quando a condição de um if é false é a cláusula ___ (uma palavra).
else provides the alternative branch. · else fornece o ramo alternativo.