Relational operators in C - Time & Space Complexity
Relational operators compare values to decide true or false. We want to see how fast these comparisons run as input changes.
How does the time to compare values grow when we use relational operators in code?
Analyze the time complexity of the following code snippet.
int compare(int a, int b) {
if (a > b) {
return 1;
} else {
return 0;
}
}
This code compares two numbers and returns 1 if the first is greater, otherwise 0.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: One relational comparison (a > b)
- How many times: Exactly once per function call
Each time we call this function, it does one comparison no matter the numbers.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 comparisons if called 10 times |
| 100 | 100 comparisons if called 100 times |
| 1000 | 1000 comparisons if called 1000 times |
Pattern observation: The number of operations grows directly with how many times the function is called, but each call is just one quick comparison.
Time Complexity: O(1)
This means the comparison takes the same short time no matter what numbers we compare.
[X] Wrong: "Comparing bigger numbers takes longer time."
[OK] Correct: Computers compare numbers in fixed steps regardless of size, so time stays constant.
Understanding that simple comparisons run in constant time helps you reason about bigger programs where many comparisons happen.
"What if we compare two arrays element by element instead of single numbers? How would the time complexity change?"
Practice
Solution
Step 1: Understand the purpose of each operator
The operator == is used to compare two values for equality. The single = is an assignment operator, != checks inequality, and > checks if left is greater than right.Step 2: Identify the equality operator
Since == compares two values for equality, it is the correct relational operator for this purpose.Final Answer:
== -> Option BQuick Check:
Equality check = == [OK]
- Confusing = (assignment) with == (equality)
- Using != instead of ==
- Using > instead of ==
a is not equal to variable b in C?Solution
Step 1: Recall the correct not equal operator in C
In C, the operator for not equal is !=. Other options like <> or !== are not valid in C.Step 2: Verify the syntax correctness
The expression a != b correctly compares if a is not equal to b.Final Answer:
a != b -> Option DQuick Check:
Not equal operator = != [OK]
- Using <> which is invalid in C
- Writing =! instead of !=
- Using !== which is from other languages
int a = 5, b = 10;
if (a > b) {
printf("True\n");
} else {
printf("False\n");
}Solution
Step 1: Evaluate the relational expression a > b
Variable a is 5 and b is 10. Since 5 is not greater than 10, the expression a > b is false.Step 2: Determine which branch executes
Because the condition is false, the else block runs, printing "False".Final Answer:
False -> Option CQuick Check:
5 > 10 = false [OK]
- Assuming 5 > 10 is true
- Confusing else and if blocks
- Ignoring relational operator meaning
int x = 7, y = 3;
if (x =< y) {
printf("x is less or equal to y\n");
}Solution
Step 1: Check the relational operator syntax
The operator =< is invalid in C. The correct operator for less than or equal is <=.Step 2: Confirm other syntax correctness
The semicolon after if condition is not needed, assignment operator is not used here, and printf syntax is correct.Final Answer:
Using =< instead of <= -> Option AQuick Check:
Less or equal operator is <=, not =< [OK]
- Swapping <= to =<
- Adding semicolon after if condition
- Confusing assignment = with comparison ==
int a = 4, b = 4, c = 5;
if (a == b && b < c) {
printf("Yes\n");
} else {
printf("No\n");
}Choose the correct output considering relational operators combined with logical operators.
Solution
Step 1: Evaluate the first condition a == b
Both a and b are 4, so a == b is true.Step 2: Evaluate the second condition b < c
b is 4 and c is 5, so b < c is true.Step 3: Combine conditions with logical AND
Both conditions are true, so the combined condition is true, executing the if block.Final Answer:
Yes -> Option AQuick Check:
(4 == 4) && (4 < 5) = true [OK]
- Ignoring logical AND meaning
- Mixing up == and =
- Assuming false when both conditions true
Ready to prove it?
Pass the test and this pattern moves unknown
