If–else statement in C++ - Time & Space Complexity
We want to see how the time it takes to run an if-else statement changes as the input changes.
Specifically, does choosing one path or the other affect how long the program runs?
Analyze the time complexity of the following code snippet.
int checkNumber(int n) {
if (n > 0) {
return 1;
} else {
return -1;
}
}
This code checks if a number is positive or not and returns a value accordingly.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: A single if-else check.
- How many times: Exactly once per function call.
The program always does one check no matter what the input number is.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 |
| 100 | 1 |
| 1000 | 1 |
Pattern observation: The number of operations stays the same even if the input changes.
Time Complexity: O(1)
This means the time to run the code does not grow with input size; it stays constant.
[X] Wrong: "If-else takes longer for bigger numbers because it has to check more conditions."
[OK] Correct: The if-else only checks one condition once, no matter how big the number is.
Understanding that simple decisions like if-else run in constant time helps you explain how your code behaves clearly and confidently.
"What if we added a loop inside the if block that runs n times? How would the time complexity change?"
Practice
if-else statement do in C++?Solution
Step 1: Understand the purpose of if-else
An if-else statement lets the program decide which block of code to run based on a condition.Step 2: Compare with other options
Repeating code is done by loops, functions define reusable code, and variables store data, so these are not correct.Final Answer:
It chooses between two paths based on a condition. -> Option CQuick Check:
If-else = choose path [OK]
- Confusing if-else with loops
- Thinking if-else defines functions
- Mixing variable declaration with control flow
Solution
Step 1: Check C++ if-else syntax rules
C++ requires parentheses around the condition and braces around blocks for multiple statements.Step 2: Analyze each option
if (x > 0) { cout << "Positive"; } else { cout << "Non-positive"; } uses correct parentheses and braces. Options A and B miss parentheses or use incorrect keywords. if (x > 0) cout << "Positive" else cout << "Non-positive"; misses semicolon after else block.Final Answer:
if (x > 0) { cout << "Positive"; } else { cout << "Non-positive"; } -> Option AQuick Check:
Correct syntax uses parentheses and braces [OK]
- Omitting parentheses around condition
- Missing braces for multiple statements
- Using 'then' keyword (not in C++)
int x = 5;
if (x < 3) {
cout << "Less";
} else {
cout << "More or equal";
}Solution
Step 1: Evaluate the condition x < 3
Variable x is 5, so 5 < 3 is false.Step 2: Determine which block runs
Since condition is false, the else block runs, printing "More or equal".Final Answer:
More or equal -> Option BQuick Check:
Condition false -> else block output [OK]
- Assuming if-block runs when condition is false
- Confusing output strings
- Ignoring else block
int n = 10;
if n > 5 {
cout << "Big";
} else {
cout << "Small";
}Solution
Step 1: Check if statement syntax
C++ requires parentheses around the condition in if statements.Step 2: Identify the error
The code uses if n > 5 without parentheses, which is invalid syntax.Final Answer:
Missing parentheses around condition -> Option AQuick Check:
if condition needs parentheses [OK]
- Omitting parentheses in if condition
- Forgetting semicolons (though not the main error here)
- Misreading else keyword
Solution
Step 1: Understand divisibility by 2
A number is divisible by 2 if remainder when divided by 2 is zero, i.e., num % 2 == 0.Step 2: Check each option's condition
if (num % 2 == 0) { cout << "Even"; } else { cout << "Odd"; } correctly uses num % 2 == 0. if (num / 2 == 0) { cout << "Even"; } else { cout << "Odd"; } uses division instead of modulo. if (num % 2 = 0) { cout << "Even"; } else { cout << "Odd"; } uses assignment (=) instead of comparison (==). if (num % 2 != 0) { cout << "Even"; } else { cout << "Odd"; } reverses the logic.Final Answer:
if (num % 2 == 0) { cout << "Even"; } else { cout << "Odd"; } -> Option DQuick Check:
Use modulo == 0 for even check [OK]
- Using assignment (=) instead of comparison (==)
- Using division (/) instead of modulo (%)
- Reversing if and else blocks
Ready to prove it?
Pass the test and this pattern moves unknown
