Relational Operators
Relational operators compare two values and return a boolean result — either true or false. They answer questions like "is this value greater than that one?" or "are these two values equal?"
1. What are Relational Operators?
Relational operators compare two values and return a boolean result — either true or false. They answer questions like "is this value greater than that one?" or "are these two values equal?"
2. Why is it used?
Programs constantly need to compare values to make decisions — like checking if a student's marks are enough to pass, or if a user's age qualifies them for something. Relational operators make these comparisons possible.
3. Real-Life Example
Think of a security guard checking if a visitor's age is 18 or above before allowing entry. The guard compares the visitor's age with 18 — this comparison is exactly what a relational operator does in code.
4. Syntax
javaa == b // equal to a != b // not equal to a > b // greater than a < b // less than a >= b // greater than or equal to a <= b // less than or equal to
5. Example Program
javapublic class RelationalDemo { public static void main(String[] args) { int age = 20; System.out.println("Is age >= 18? " + (age >= 18)); System.out.println("Is age == 25? " + (age == 25)); } }
Output:
Is age >= 18? true
Is age == 25? false6. Key Points to Remember
- Relational operators always produce a
booleanvalue (trueorfalse). - Don't confuse
==(comparison) with=(assignment) — this is a very common beginner mistake. - These operators are heavily used inside
ifconditions and loops.