Integer Comparison: What It Means and Why It Matters
Imagine you have two piles of stones. One pile has 5 stones, the other has 3. Without counting, you can see the first pile is bigger. That's comparison in the real world — you're asking "which is larger?" or "are they equal?"
Integer comparison is just that same question, but asked about whole numbers (…, -3, -2, -1, 0, 1, 2, 3, …). The only twist is that integers include negative numbers, so the "bigger" pile might actually be a debt or a temperature below zero.
The Intuition: The Number Line
Picture a horizontal line. In the middle is 0. To the right are positive numbers (1, 2, 3, …), getting larger as you go right. To the left are negative numbers (-1, -2, -3, …), getting more negative (smaller) as you go left.
The golden rule of integer comparison
On a number line, the number farther to the right is always greater.
So:
- 5 is to the right of 3 → 5 > 3
- -2 is to the right of -5 → -2 > -5
- 0 is to the right of -1 → 0 > -1
The Precise Statement
For any two integers a and b:
- a>b (a is greater than b) if a lies to the right of b on the number line.
- a<b (a is less than b) if a lies to the left of b on the number line.
- a=b if a and b are the same point on the number line.
Equivalently, in terms of subtraction:
- a>b means a−b is positive.
- a<b means a−b is negative.
- a=b means a−b=0.
Common Pitfalls
The "bigger negative" trap …