If statement in C++ - Time & Space Complexity
We want to see how the time it takes to run an if statement changes as the input changes.
Does the if statement make the program slower when the input grows?
Analyze the time complexity of the following code snippet.
int checkNumber(int n) {
if (n > 0) {
return 1;
} else {
return 0;
}
}
This code checks if a number is positive and returns 1 if true, otherwise 0.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: A single if condition check.
- How many times: Exactly once per function call.
The if statement runs once no matter what the input number is.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 |
| 100 | 1 |
| 1000 | 1 |
Pattern observation: The time stays the same even if the input number grows.
Time Complexity: O(1)
This means the if statement takes the same amount of time no matter how big the input is.
[X] Wrong: "If statements take longer when the input number is bigger."
[OK] Correct: The if statement only checks the condition once, so input size does not affect its speed.
Understanding that simple decisions like if statements run in constant time helps you explain how your code behaves clearly and confidently.
"What if we added a loop that runs n times around the if statement? How would the time complexity change?"
Practice
if statement do in C++?Solution
Step 1: Understand the purpose of
Anifstatementifstatement checks a condition and runs code only when that condition is true.Step 2: Compare with other options
Repeating code is done by loops, defining variables is done by declarations, and ending the program is done byreturnorexit.Final Answer:
Runs code only if a condition is true -> Option CQuick Check:
If statement = runs code if true [OK]
- Confusing if with loops
- Thinking if defines variables
- Believing if ends program
if statement in C++?Solution
Step 1: Recall correct
In C++, the condition must be inside parentheses and the code block inside curly braces.ifsyntaxStep 2: Check each option
if (condition) { cout << "Yes"; } uses parentheses and curly braces correctly. if condition { cout << "Yes"; } misses parentheses. if condition: cout << "Yes"; uses colon which is invalid in C++. if (condition) cout << "Yes" misses curly braces which is allowed only for single statements but here it is incomplete.Final Answer:
if (condition) { cout << "Yes"; } -> Option DQuick Check:
Parentheses + braces = correct if syntax [OK]
- Missing parentheses
- Using colon instead of braces
- Omitting braces for multiple statements
int x = 10;
if (x > 5) {
cout << "Hello";
} else {
cout << "Bye";
}Solution
Step 1: Evaluate the condition
Sincex > 5xis 10, which is greater than 5, the condition is true.Step 2: Determine which block runs
Because the condition is true, the code inside theifblock runs, printing "Hello".Final Answer:
Hello -> Option AQuick Check:
10 > 5 is true, prints Hello [OK]
- Confusing else output
- Ignoring condition result
- Assuming both blocks run
int a = 3;
if a > 0 {
cout << "Positive";
}Solution
Step 1: Check the
The condition in anifstatement syntaxifstatement must be inside parentheses.Step 2: Identify the error
The code writesif a > 0 {without parentheses, which is invalid syntax.Final Answer:
Condition must be inside parentheses -> Option BQuick Check:
If condition needs parentheses [OK]
- Omitting parentheses
- Thinking semicolon needed after if
- Assuming braces optional always
if statement to do this?int num = 7; // Your code here
Solution
Step 1: Understand how to check even numbers
A number is even if dividing by 2 leaves remainder 0, checked bynum % 2 == 0.Step 2: Analyze each option
if (num % 2 == 0) { cout << "Even"; } else { cout << "Odd"; } correctly usesnum % 2 == 0. if (num / 2 == 0) { cout << "Even"; } else { cout << "Odd"; } uses division instead of modulo, which is wrong. 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 AQuick Check:
Modulo 2 equals 0 means even [OK]
- Using division instead of modulo
- Using = instead of ==
- Reversing condition logic
Ready to prove it?
Pass the test and this pattern moves unknown
