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 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.
Checking the if condition takes the same time no matter what number we give.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 check |
| 100 | 1 check |
| 1000 | 1 check |
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 condition is just one check and does not repeat or grow with input size.
Understanding that simple decisions like if statements run quickly helps you explain how your code behaves in real situations.
"What if we added a loop inside the if statement? How would the time complexity change?"
Practice
if statement do in C programming?Solution
Step 1: Understand the purpose of an if statement
An if statement checks a condition and decides whether to run the code inside it.Step 2: Compare options with the if statement behavior
Only It runs code only if a condition is true. correctly describes that the code runs only if the condition is true.Final Answer:
It runs code only if a condition is true. -> Option CQuick Check:
If statement = runs code if true [OK]
- Confusing if with loops
- Thinking if stores data
- Mixing if with function definition
Solution
Step 1: Recall correct if statement syntax in C
The condition must be inside parentheses, and the code block inside braces.Step 2: Check each option
if (condition) { printf("Hello"); } uses parentheses for condition and braces for code block, which is correct syntax.Final Answer:
if (condition) { printf("Hello"); } -> Option AQuick Check:
Parentheses + braces = correct if syntax [OK]
- Omitting parentheses around condition
- Using colon instead of braces
- Missing semicolon after printf
int x = 5;
if (x > 3) {
printf("Yes");
} else {
printf("No");
}Solution
Step 1: Evaluate the condition x > 3
Since x is 5, 5 > 3 is true.Step 2: Determine which block runs
The if block runs and prints "Yes" because the condition is true.Final Answer:
Yes -> Option AQuick Check:
5 > 3 is true, prints Yes [OK]
- Confusing else block execution
- Assuming syntax error without checking code
- Ignoring condition evaluation
int a = 10;
if a > 5 {
printf("Big");
}Solution
Step 1: Check if statement syntax
The condition in an if statement must be inside parentheses in C.Step 2: Identify the error in the code
The code has if a > 5 { ... } without parentheses around a > 5, which is incorrect.Final Answer:
Condition must be inside parentheses -> Option DQuick Check:
Condition needs parentheses [OK]
- Using braces instead of parentheses for condition
- Forgetting semicolon after statements
- Assuming variable not declared
int n = 0;
if (n) {
printf("True");
} else {
printf("False");
}What will be printed and why?
Solution
Step 1: Understand how C treats integers in conditions
In C, zero means false, any nonzero means true.Step 2: Evaluate the condition if (n)
Since n is 0, the condition is false, so else block runs.Step 3: Determine output
The else block prints "False".Final Answer:
False, because zero is false in C -> Option BQuick Check:
Zero = false, prints False [OK]
- Thinking zero is true
- Expecting boolean type only
- Assuming program error
Ready to prove it?
Pass the test and this pattern moves unknown
