If–else statement in C - Time & Space Complexity
We want to see how the time to run an if-else statement changes as the input changes.
Does the program take longer if the input is bigger?
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 or 0 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 time to run this code does not change with the size of the input number.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 check |
| 100 | 1 check |
| 1000 | 1 check |
Pattern observation: The number of operations stays the same no matter how big the input is.
Time Complexity: O(1)
This means the time to run the if-else statement stays constant, no matter the input size.
[X] Wrong: "If the input number is bigger, the if-else will take longer to decide."
[OK] Correct: The if-else only checks the condition once, so the input size does not affect the time.
Understanding that simple if-else checks run in constant time helps you explain how decisions in code affect performance.
"What if we added a loop inside the else 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 pick one of two paths depending on whether a condition is true or false.Step 2: Match the description to the options
Only It chooses between two actions based on a condition. describes choosing between two actions based on a condition, which is exactly what if-else does.Final Answer:
It chooses between two actions based on a condition. -> Option DQuick Check:
If-else = choose between two paths [OK]
- Confusing if-else with loops
- Thinking if-else declares variables
- Mixing if-else with function definitions
Solution
Step 1: Recall C syntax rules for if-else
In C, conditions must be inside parentheses and braces {} are used for code blocks.Step 2: Check each option
Only if (condition) { /* code */ } else { /* code */ } uses parentheses around the condition and braces correctly without extra keywords like 'then'.Final Answer:
if (condition) { /* code */ } else { /* code */ } -> Option CQuick Check:
Correct syntax uses parentheses and braces [OK]
- Omitting parentheses around condition
- Using 'then' keyword (not in C)
- Skipping braces for multiple statements
#include
int main() {
int x = 10;
if (x > 5) {
printf("Greater\n");
} else {
printf("Smaller\n");
}
return 0;
}Solution
Step 1: Evaluate the condition x > 5
Variable x is 10, which is greater than 5, so the condition is true.Step 2: Determine which block runs
Since the condition is true, the code inside the if block runs, printing "Greater".Final Answer:
Greater -> Option AQuick Check:
10 > 5 is true, so prints "Greater" [OK]
- Confusing which block runs on true condition
- Ignoring the newline character in printf
- Assuming else block runs when condition is true
int a = 3;
if a > 0
printf("Positive\n");
else
printf("Non-positive\n");Solution
Step 1: Check syntax of if condition
In C, the condition in an if statement must be inside parentheses (). Here, it is missing.Step 2: Verify other parts
Semicolons after printf are present, braces are optional for single statements, and else keyword is correct.Final Answer:
Missing parentheses around condition in if statement -> Option BQuick Check:
if condition needs parentheses ( ) [OK]
- Forgetting parentheses in if condition
- Thinking braces are always required for single statements
- Misplacing else keyword
Solution
Step 1: Understand how to check even numbers
A number is even if dividing by 2 leaves remainder 0, which is checked by num % 2 == 0.Step 2: Analyze each option
if (num % 2 == 0) { printf("Even\n"); } else { printf("Odd\n"); } correctly uses modulus operator and equality check. if (num / 2 == 0) { printf("Even\n"); } else { printf("Odd\n"); } uses division instead of modulus. if (num % 2 = 0) { printf("Even\n"); } else { printf("Odd\n"); } uses assignment (=) instead of comparison (==). if (num % 2 != 0) { printf("Even\n"); } else { printf("Odd\n"); } reverses the logic.Final Answer:
if (num % 2 == 0) { printf("Even\n"); } else { printf("Odd\n"); } -> Option AQuick Check:
Use modulus == 0 to check even [OK]
- Using division instead of modulus
- Using = instead of == in condition
- Reversing even and odd logic
Ready to prove it?
Pass the test and this pattern moves unknown
