0
0
Cprogramming~10 mins

If statement in C - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - If statement
Start
Evaluate condition
Condition True?
NoSkip if block
Execute if block
Continue
End
The program checks a condition. If true, it runs the code inside the if block. Otherwise, it skips it and continues.
Execution Sample
C
#include <stdio.h>

int main() {
    int x = 5;
    if (x > 3) {
        printf("x is greater than 3\n");
    }
    return 0;
}
Checks if x is greater than 3 and prints a message if true.
Execution Table
StepConditionResultBranch TakenOutput
1x > 3TrueExecute if blockx is greater than 3
2End of ifN/AContinue
💡 Condition is true, so the if block runs once, then execution continues.
Variable Tracker
VariableStartAfter ifFinal
x555
Key Moments - 2 Insights
Why does the code inside the if block run only when the condition is true?
Because the if statement checks the condition first (see execution_table step 1). If the condition is true, it runs the block; otherwise, it skips it.
What happens if the condition is false?
The program skips the if block entirely and continues after it, as shown by the 'Skip if block' path in the concept_flow.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output at step 1?
Ax is less than 3
BNo output
Cx is greater than 3
DError
💡 Hint
Check the Output column in execution_table row 1.
At which step does the program decide to skip the if block if the condition is false?
AStep 2
BStep 1
CBefore Step 1
DThere is no skip step
💡 Hint
Look at the Branch Taken column in execution_table step 1.
If x was 2 instead of 5, what would happen in the execution table?
ACondition would be false and no output
BProgram would crash
CCondition would be true and output printed
DOutput would be different text
💡 Hint
Refer to the Condition and Output columns in execution_table step 1.
Concept Snapshot
If statement syntax:
if (condition) {
    // code runs if condition is true
}

Checks condition once.
Runs block only if true.
Skips block if false.
Full Transcript
This visual trace shows how an if statement works in C. The program starts and evaluates the condition inside the if. If the condition is true, it executes the code inside the if block, printing a message. If false, it skips the block and continues. The variable x remains unchanged throughout. The execution table shows each step, the condition result, and what branch the program takes. This helps beginners see exactly when and why the if block runs or is skipped.