0
0
C++programming~10 mins

If statement in C++ - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - If statement
Start
Evaluate condition
Execute if-block
Continue
Skip if-block
Continue
The program checks a condition. If true, it runs the code inside the if-block; if false, it skips it and continues.
Execution Sample
C++
#include <iostream>
using namespace std;

int main() {
    int x = 5;
    if (x > 3) {
        cout << "x is greater than 3" << endl;
    }
    cout << "Done" << endl;
    return 0;
}
Checks if x is greater than 3, prints a message if true, then prints "Done".
Execution Table
StepCondition (x > 3)ResultBranch TakenOutput
15 > 3TrueExecute if-blockx is greater than 3
2N/AN/AContinue after ifDone
💡 After executing the if-block, program continues to print "Done" and ends.
Variable Tracker
VariableStartAfter Step 1After Step 2
x555
Key Moments - 2 Insights
Why does the program print "x is greater than 3"?
Because the condition x > 3 is true at step 1, so the if-block runs and prints the message.
What happens if the condition is false?
The program skips the if-block and directly continues to the next statement, as shown in step 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the condition result at step 1?
AFalse
BTrue
CUndefined
DZero
💡 Hint
Check the 'Condition (x > 3)' column at step 1 in the execution table.
At which step does the program print "Done"?
AStep 1
BBefore step 1
CStep 2
DNever
💡 Hint
Look at the 'Output' column in the execution table for when "Done" is printed.
If x was 2 instead of 5, what would happen at step 1?
ACondition false, if-block skipped
BProgram crashes
CCondition true, if-block runs
DOutput prints twice
💡 Hint
Refer to the condition x > 3 and what happens when it is false in the key moments.
Concept Snapshot
If statement syntax:
if (condition) {
    // code runs if condition true
}

Checks condition once.
Runs block only if true.
Skips block if false.
Then continues program.
Full Transcript
This visual trace shows how an if statement works in C++. The program starts by evaluating the condition x > 3. Since x is 5, the condition is true, so the code inside the if-block runs and prints "x is greater than 3". Then the program continues and prints "Done". If the condition were false, the if-block would be skipped and only "Done" would print. Variables remain unchanged during this process.