Nested conditional statements - Time & Space Complexity
We want to see how the time to run nested conditional statements changes as input changes.
Are these conditions making the program slower when inputs grow?
Analyze the time complexity of the following code snippet.
int checkValue(int x) {
if (x > 0) {
if (x % 2 == 0) {
return 1;
} else {
return 2;
}
} else {
return 3;
}
}
This code checks a number and returns different results based on nested conditions.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Single check of conditions (if-else statements)
- How many times: Exactly once per function call
The program only checks conditions once, no matter the input size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 2-3 checks |
| 100 | 2-3 checks |
| 1000 | 2-3 checks |
Pattern observation: The number of operations stays the same regardless of input size.
Time Complexity: O(1)
This means the time to run does not grow with input size; it stays constant.
[X] Wrong: "Nested ifs always make the program slower as input grows."
[OK] Correct: Nested conditions run only once per input, so they do not slow down with bigger inputs.
Understanding how conditions affect time helps you explain your code clearly and shows you know what really costs time in programs.
"What if we added a loop around these nested conditions? How would the time complexity change?"
Practice
What does a nested conditional statement in C allow you to do?
Choose the best description.
Solution
Step 1: Understand the meaning of nested conditionals
Nested conditionals mean putting one if or else if statement inside another if or else if block.Step 2: Compare with other options
Repeating code is done by loops, not conditionals. Functions inside functions or variable type changes are unrelated.Final Answer:
Check a condition inside another condition -> Option BQuick Check:
Nested conditionals = condition inside condition [OK]
- Confusing nested conditionals with loops
- Thinking nested means changing variable types
- Mixing nested conditionals with function definitions
Which of the following is the correct syntax for a nested if statement in C?
if (x > 0) {
if (y < 10) {
// code
}
}Solution
Step 1: Check correct use of braces and parentheses
In C, conditions must be inside parentheses and blocks inside braces. if (x > 0) { if (y < 10) { /* code */ } } uses both correctly.Step 2: Identify syntax errors in other options
if (x > 0) if (y < 10) { /* code */ } misses braces for outer if block. if (x > 0) { if y < 10 { /* code */ } } misses parentheses around y < 10. if x > 0 { if (y < 10) { /* code */ } } misses parentheses around x > 0.Final Answer:
if (x > 0) { if (y < 10) { /* code */ } } -> Option DQuick Check:
Correct syntax = if (x > 0) { if (y < 10) { /* code */ } } [OK]
- Omitting parentheses around conditions
- Forgetting braces for code blocks
- Mixing syntax from other languages
What is the output of this C code?
int x = 5, y = 8;
if (x > 0) {
if (y < 10) {
printf("A\n");
} else {
printf("B\n");
}
} else {
printf("C\n");
}Solution
Step 1: Evaluate outer if condition
x = 5, which is greater than 0, so the outer if block runs.Step 2: Evaluate inner if condition
y = 8, which is less than 10, so the inner if block runs and prints "A".Final Answer:
A -> Option AQuick Check:
x > 0 and y < 10 prints A [OK]
- Ignoring inner condition
- Confusing else blocks
- Assuming no output if outer condition true
Find the error in this nested conditional code snippet:
if (a > 10)
if (b < 5)
printf("X\n");
else
printf("Y\n");Solution
Step 1: Analyze the if-else pairing
Without braces, the else pairs with the nearest if, which is the inner if. This may cause confusion or unexpected behavior.Step 2: Identify the fix
Adding braces around the outer if block ensures the else pairs correctly and code is clear.Final Answer:
Missing braces causing else to pair with wrong if -> Option AQuick Check:
Braces fix else pairing [OK]
- Assuming else pairs with outer if
- Ignoring braces importance
- Thinking semicolons cause this error
Given the code below, what will be printed if score = 75?
if (score >= 90) {
printf("Grade A\n");
} else {
if (score >= 70) {
if (score >= 80) {
printf("Grade B+\n");
} else {
printf("Grade B\n");
}
} else {
printf("Grade C\n");
}
}Solution
Step 1: Check first if condition
score = 75, which is not >= 90, so skip first if block.Step 2: Check second if condition
score >= 70 is true, so enter second if block.Step 3: Check nested if inside second block
score >= 80 is false (75 < 80), so else block runs printing "Grade B".Final Answer:
Grade B -> Option CQuick Check:
75 >= 70 but < 80 prints Grade B [OK]
- Confusing >= 70 and >= 80 checks
- Skipping nested else block
- Assuming 75 is Grade B+
Ready to prove it?
Pass the test and this pattern moves unknown
