0
0
Javaprogramming~10 mins

If–else statement in Java - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - If–else statement
Start
Evaluate condition
Execute if-block
End if-block
Execute else-block
End else-block
Continue with rest of program
The program checks a condition. If true, it runs the 'if' part; if false, it runs the 'else' part. Then it continues.
Execution Sample
Java
int x = 10;
if (x > 5) {
    System.out.println("x is greater than 5");
} else {
    System.out.println("x is 5 or less");
}
Checks if x is greater than 5 and prints a message accordingly.
Execution Table
StepCondition (x > 5)ResultBranch TakenOutput
110 > 5trueif-blockx is greater than 5
2End of if–else--Program continues
💡 Condition true, executed if-block, then program continues
Variable Tracker
VariableStartAfter Condition CheckFinal
x101010
Key Moments - 2 Insights
Why does the program not run the else-block when x is 10?
Because the condition x > 5 is true (see execution_table step 1), so only the if-block runs.
What happens if the condition is false?
The program skips the if-block and runs the else-block instead, as shown in the concept flow.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output at step 1?
A"x is greater than 5"
B"x is 5 or less"
CNo output
DError
💡 Hint
Check the 'Output' column in execution_table row 1.
At which step does the program decide which branch to take?
AStep 2
BBefore Step 1
CStep 1
DAfter Step 2
💡 Hint
Look at the 'Condition' and 'Branch Taken' columns in execution_table.
If x was 3 instead of 10, which branch would run?
Aif-block
Belse-block
CBoth if and else blocks
DNeither block
💡 Hint
Refer to the concept_flow and key_moments about condition false case.
Concept Snapshot
if (condition) {
  // code runs if condition true
} else {
  // code runs if condition false
}
Checks condition once, runs one branch only.
Full Transcript
This example shows how an if–else statement works in Java. The program sets x to 10. It checks if x is greater than 5. Since 10 is greater than 5, it runs the code inside the if-block and prints "x is greater than 5". It skips the else-block. Then the program continues. If the condition was false, it would run the else-block instead. This way, the program chooses one path based on the condition.