Relational and Logical Operators: How to Compare Data?
Understand how comparison operators work and how to combine conditions using AND, OR, and NOT. Essential knowledge for control statements.
Relational and Logical Operators
For a program to make a decision, it must first be able to compare data. Relational (comparison) operators are used for this. If we want to check multiple conditions at once, we use logical operators. These are the heart of every if statement and loop.
[Image showing comparison and logical operators symbols in code]
1. Relational Operators (Comparisons)
Used to check the relationship between two values. The result is always a boolean: true or false.
2. Logical Operators
Allow combining multiple comparisons into one complex expression.
| Cecha | Operator | Name |
|---|---|---|
| && | AND | True if both are true |
| || | OR | True if at least one is true |
| ! | NOT | Reverses the boolean value |
Common Mistakes and Pitfalls
Confusing = with ==
Quiz: Check your knowledge
What is the result of: (5 > 2) || (3 < 1) ?
What next?
- Memorize truth tables for AND, OR, and XOR operators.
- Practice writing conditions checking numeric ranges.
- Learn the difference between
==and===in JavaScript.
You might also like
Conditional Operator: Short-hand if-else in one line
Learn the ternary operator. Find out how to shorten your code and when to use it instead of classic if-else statements.
Mathematical Operators: The Foundation of Computing
Learn basic and advanced mathematical operators. Understand how modulo, incrementation works, and why operator precedence matters.
Reserved Keywords: List of Key Terms in IT
Understand what keywords are in programming languages. Learn why you can't use them as variable names and see lists for C++, Java, and Python.