0
0
Cprogramming~10 mins

Nested conditional statements - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Nested conditional statements
Start
Check Condition A
|Yes
Check Condition B
|Yes
Execute Block 1
End
No
Execute Block 2
End
No
Execute Block 3
End
First, check the outer condition. If true, check the inner condition. Depending on inner condition, execute one block. If outer condition is false, execute another block.
Execution Sample
C
int x = 10;
int y = 20;
if (x > 5) {
  if (y > 15) {
    printf("Both conditions true\n");
  } else {
    printf("Only x > 5\n");
  }
} else {
  printf("x <= 5\n");
}
This code checks if x is greater than 5, then inside that, checks if y is greater than 15, printing messages accordingly.
Execution Table
StepCondition CheckedResultBranch TakenOutput
1x > 5 (10 > 5)TrueEnter inner if
2y > 15 (20 > 15)TruePrint 'Both conditions true'Both conditions true
3End of if-else---
💡 All conditions evaluated; program ends after printing message.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
x10101010
y20202020
Key Moments - 2 Insights
Why does the program check y > 15 only after confirming x > 5?
Because the inner if is nested inside the outer if, so y > 15 is checked only if x > 5 is true (see execution_table step 1 and 2).
What happens if x <= 5?
The program skips the inner if and executes the else block for the outer if (not shown in this run, but would print 'x <= 5').
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output at step 2?
A"x <= 5"
B"Only x > 5"
C"Both conditions true"
DNo output
💡 Hint
Check the Output column at step 2 in the execution_table.
At which step does the program decide not to check y > 15?
AStep 2
BStep 1
CStep 3
DNever
💡 Hint
If x > 5 is false at step 1, inner condition y > 15 is skipped.
If x was 3, what would be the output?
A"x <= 5"
B"Only x > 5"
C"Both conditions true"
DNo output
💡 Hint
Refer to key_moments about what happens if x <= 5.
Concept Snapshot
Nested conditionals check one condition inside another.
Syntax: if (cond1) { if (cond2) { ... } else { ... } } else { ... }
Inner condition runs only if outer is true.
Use to test multiple related conditions step-by-step.
Full Transcript
This example shows nested conditional statements in C. First, the program checks if x is greater than 5. If yes, it then checks if y is greater than 15. Depending on these checks, it prints different messages. The execution table traces these steps, showing conditions checked, results, branches taken, and output. Variables x and y remain unchanged during execution. Key moments clarify why inner conditions depend on outer ones and what happens if the outer condition is false. The quiz tests understanding of output and flow based on the execution trace.