Comparison operators (-eq, -ne, -gt, -lt) in PowerShell - Time & Space Complexity
We want to understand how the time it takes to run comparison operators changes as we compare more items.
How does the number of comparisons affect the total work done?
Analyze the time complexity of the following code snippet.
foreach ($item in $array) {
if ($item -gt 10) {
Write-Output "$item is greater than 10"
}
}
This code checks each item in an array to see if it is greater than 10 and prints a message if true.
- Primary operation: Comparison using
-gtinside a loop. - How many times: Once for each item in the array.
As the array gets bigger, the number of comparisons grows in the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 comparisons |
| 100 | 100 comparisons |
| 1000 | 1000 comparisons |
Pattern observation: The number of comparisons grows directly with the number of items.
Time Complexity: O(n)
This means the time to finish grows in a straight line as the number of items increases.
[X] Wrong: "Comparison operators take longer as numbers get bigger."
[OK] Correct: The time to compare two numbers stays about the same no matter how big the numbers are.
Understanding how simple comparisons scale helps you reason about loops and conditions in scripts, a key skill for writing efficient automation.
"What if we compared every item to every other item using nested loops? How would the time complexity change?"
Practice
Solution
Step 1: Understand the meaning of each operator
-eq means equals, -ne means not equals, -gt means greater than, -lt means less than.Step 2: Identify the operator for equality
The operator that checks if two values are equal is -eq.Final Answer:
-eq -> Option AQuick Check:
Equals operator = -eq [OK]
- Confusing -eq with -ne
- Mixing -gt and -lt
- Using == instead of -eq
Solution
Step 1: Recall PowerShell comparison syntax
PowerShell uses -gt for greater than, not > like some other languages.Step 2: Match the correct operator
The correct syntax to check if $a is greater than 10 is if ($a -gt 10) { }.Final Answer:
if ($a -gt 10) { } -> Option AQuick Check:
Greater than operator = -gt [OK]
- Using > instead of -gt
- Confusing -lt with -gt
- Missing parentheses in if statement
$x = 5
if ($x -ne 5) { 'No' } else { 'Yes' }
Solution
Step 1: Evaluate the condition $x -ne 5
$x is 5, so $x -ne 5 means 5 not equal to 5, which is False.Step 2: Determine which block runs
Since condition is False, the else block runs, outputting 'Yes'.Final Answer:
Yes -> Option CQuick Check:
5 not equal 5 is False, so else runs [OK]
- Thinking -ne means equal
- Confusing output with variable value
- Ignoring else block
$num = 10
if ($num -gt 5) { 'Greater' } elseif ($num -lt 5) { 'Smaller' } else { 'Equal' }
Solution
Step 1: Check syntax of if-elseif-else
PowerShell supports elseif as one word; braces are balanced.Step 2: Verify operators and structure
-gt and -lt are correct operators; no syntax errors present.Final Answer:
No error, code runs correctly -> Option BQuick Check:
elseif is valid in PowerShell [OK]
- Thinking elseif is invalid
- Expecting > instead of -gt
- Missing braces confusion
Solution
Step 1: Understand the requirement
We want to check if $score is greater than or equal to 70.Step 2: Identify the correct operator
-ge means greater than or equal to, which matches the requirement.Final Answer:
if ($score -ge 70) { 'Valid' } else { 'Invalid' } -> Option DQuick Check:
Greater or equal operator = -ge [OK]
- Using -gt excludes 70
- Using -eq only matches exactly 70
- Using -lt reverses logic
Ready to prove it?
Pass the test and this pattern moves unknown
