Logical operators - Time & Space Complexity
Logical operators combine conditions to decide true or false results.
We want to see how using these operators affects how long the program takes to run.
Analyze the time complexity of the following code snippet.
#include <stdio.h>
int check(int a, int b, int c) {
if ((a && b) || c) {
return 1;
}
return 0;
}
This code checks if certain conditions with logical AND and OR are true.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Single evaluation of logical conditions.
- How many times: Once per function call, no loops or repeated checks inside.
Since the code only checks a few values once, the time does not grow with input size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 3 checks |
| 100 | 3 checks |
| 1000 | 3 checks |
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 does not change as input size changes; it stays constant.
[X] Wrong: "Logical operators make the program slower as input grows because they combine many conditions."
[OK] Correct: Logical operators here only check a fixed number of values once, so the time does not increase with input size.
Understanding how simple condition checks work helps you explain code efficiency clearly and confidently.
"What if the conditions were inside a loop running n times? How would the time complexity change?"
Practice
Solution
Step 1: Understand logical AND operator
The logical AND operator (&&) returns true only if both conditions it connects are true.Step 2: Compare with other operators
The || operator means OR, ! means NOT, and & is bitwise AND, not logical AND.Final Answer:
&& -> Option CQuick Check:
AND = && [OK]
- Confusing & (bitwise) with && (logical)
- Mixing OR and AND operators
- Using ! for AND
Solution
Step 1: Identify negation syntax in C
The NOT operator in C is a single exclamation mark placed before the condition: !condition.Step 2: Check other options
condition ! places ! after condition which is invalid, C uses 'not' which is not C syntax, D is a comparison, not negation.Final Answer:
!condition -> Option BQuick Check:
NOT = ! before condition [OK]
- Putting ! after condition
- Using 'not' keyword
- Confusing != with negation
int a = 5, b = 10;
if (a > 3 && b < 15) {
printf("True\n");
} else {
printf("False\n");
}
Solution
Step 1: Evaluate first condition (a > 3)
a is 5, which is greater than 3, so this condition is true.Step 2: Evaluate second condition (b < 15)
b is 10, which is less than 15, so this condition is true.Step 3: Apply logical AND (&&)
Both conditions are true, so the combined condition is true, entering the if block.Final Answer:
True -> Option DQuick Check:
True && True = True [OK]
- Assuming OR instead of AND
- Misreading variable values
- Ignoring logical operator precedence
int x = 0;
if (x = 5 || x == 0) {
printf("Yes\n");
}
Solution
Step 1: Check the if condition syntax
The condition uses 'x = 5 || x == 0'. The '=' is assignment, not comparison.Step 2: Understand effect of assignment in condition
Assignment returns the assigned value (5), which is true, so condition always true, likely a bug.Final Answer:
Using assignment '=' instead of comparison '==' in if condition -> Option AQuick Check:
Use '==' for comparison, not '=' [OK]
- Confusing '=' and '=='
- Ignoring logical operator precedence
- Assuming code won't compile
int a = 0, b = 1, c = 2;
if (!(a || b) && c > 1) {
printf("Inside if\n");
} else {
printf("Inside else\n");
}
Solution
Step 1: Evaluate inner OR condition (a || b)
a is 0 (false), b is 1 (true), so a || b is true.Step 2: Apply NOT operator !(a || b)
NOT true is false, so this part is false.Step 3: Evaluate c > 1
c is 2, which is greater than 1, so true.Step 4: Combine with AND: false && true
False AND anything is false, so condition is false, else block runs.Final Answer:
Inside else -> Option AQuick Check:
!(true) && true = false [OK]
- Ignoring NOT operator effect
- Misreading OR condition
- Assuming if block runs when condition false
Ready to prove it?
Pass the test and this pattern moves unknown
