0
0
C++programming~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
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 <iostream>
using namespace std;

int main() {
  int x = 10;
  if (x > 5) {
    cout << "Big";
  } else {
    cout << "Small";
  }
  return 0;
}
Checks if x is greater than 5; prints 'Big' if true, otherwise 'Small'.
Execution Table
StepCondition (x > 5)ResultBranch TakenOutput
110 > 5trueif-block"Big"
2--End-
💡 Condition true, executed if-block, then program ends.
Variable Tracker
VariableStartAfter Step 1Final
x101010
Key Moments - 2 Insights
Why does the program print "Big" and not "Small"?
Because the condition x > 5 is true at step 1, so the if-block runs and else-block is skipped.
What happens if the condition is false?
The else-block runs instead, printing "Small" as shown in the branch taken column if condition was false.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output at step 1?
A"Small"
B"Big"
CNo output
DError
💡 Hint
Check the Output column at step 1 in the 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 the execution_table.
If x was 3 instead of 10, what would the output be?
A"Small"
B"Big"
CNo output
DError
💡 Hint
Refer to the key_moments explanation about condition false leading to else-block.
Concept Snapshot
if (condition) {
  // code if true
} else {
  // code if false
}

Checks condition once; runs if-block if true, else-block if false.
Full Transcript
This example shows how an if-else statement works in C++. The program starts by checking if x is greater than 5. Since x is 10, the condition is true, so it runs the code inside the if-block and prints "Big". If the condition were false, it would run the else-block and print "Small". The execution table shows each step: evaluating the condition, choosing the branch, and printing the output. The variable tracker shows that x stays 10 throughout. This helps beginners see how the program decides which code to run based on a condition.