0
0
Cprogramming~10 mins

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

Choose your learning style9 modes available
Concept Flow - If–else statement
Start
Evaluate condition
Yes No
Execute if-block
End
Execute else-block
End
The program checks a condition. If true, it runs the 'if' part; if false, it runs the 'else' part, then ends.
Execution Sample
C
#include <stdio.h>

int main() {
    int x = 10;
    if (x > 5) {
        printf("Greater\n");
    } else {
        printf("Smaller or equal\n");
    }
    return 0;
}
Checks if x is greater than 5 and prints a message accordingly.
Execution Table
StepCondition (x > 5)ResultBranch TakenOutput
110 > 5Trueif-block"Greater\n" printed
2End of if-else--Program ends
💡 Condition true, so if-block executed; else-block skipped.
Variable Tracker
VariableStartAfter Condition CheckFinal
x101010
Key Moments - 2 Insights
Why does the else-block not run even though it exists?
Because the condition (x > 5) is true at step 1 in the execution_table, so only the if-block runs.
What happens if the condition is false?
The else-block runs instead, as shown by the 'Branch Taken' column in the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output at step 1?
ANo output
B"Greater\n" printed
C"Smaller or equal\n" printed
DError message
💡 Hint
Check the 'Output' column in the first row of execution_table.
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 row 1.
If x was 3 instead of 10, which branch would be taken?
Aelse-block
Bif-block
CBoth if and else blocks
DNeither block
💡 Hint
Refer to the condition (x > 5) and what happens when it is false.
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 C. The program sets x to 10, then checks if x is greater than 5. Since 10 is greater than 5, it prints "Greater" and skips the else part. The flow diagram shows the decision path. The execution table tracks the condition check and output. Variables remain unchanged. Key points clarify why only one branch runs. The quiz tests understanding of output, decision step, and behavior if x changes.