Operators: Relational, Logical, and Arithmetic
Master the essential tools that power all programming operations!
The Tools of the Trade
Operators are symbols used to perform specific mathematical or logical manipulations on data (operands). Think of them as the "verbs" of programming — they tell the computer what action to perform.
The Three Categories
Arithmetic Operators
Used for calculations like addition, subtraction, multiplication, and division
Relational Operators
Used to compare two values, always returning True or False
Logical Operators
Used to combine multiple conditions for complex decision-making
Click on each symbol to sort it into the correct category!
Arithmetic
Relational
Logical
Arithmetic Operators (Calculations)
Arithmetic operators perform basic mathematical calculations on numbers.
The Basics
+ Addition
Adds two values (e.g., 5 + 3 = 8)
- Subtraction
Subtracts one value from another (e.g., 10 - 4 = 6)
* Multiplication
Multiplies two values (e.g., 6 * 7 = 42)
/ Division
Divides one value by another (e.g., 15 / 3 = 5)
Integer Division & Remainder
When working with whole numbers, two additional operators become important:
DIV returns the whole number part of division (ignores the remainder).
MOD returns just the remainder after division.
Order of Operations (BODMAS/PEMDAS)
When an expression has multiple operators, they are evaluated in this order:
- Brackets/Parentheses - Calculations inside () are done first
- Orders/Exponents - Powers and square roots
- Division & Multiplication - Left to right
- Addition & Subtraction - Left to right
Enter two numbers to see how DIV and MOD work!
Relational Operators (Comparisons)
Relational operators compare two values. The result is always a Boolean — either True or False.
The Comparison Symbols
> Greater than
True if left is greater (e.g., 10 > 5 = True)
< Less than
True if left is smaller (e.g., 3 < 8 = True)
>= Greater than or equal
True if left is greater or equal (e.g., 5 >= 5 = True)
<= Less than or equal
True if left is smaller or equal (e.g., 7 <= 10 = True)
= Equal to
True if both values are equal (e.g., 6 = 6 = True)
<> Not equal to
True if values are different (e.g., 4 <> 9 = True)
Click True or False as fast as you can! Beat the timer!
Logical Operators (Joining Decisions)
Logical operators combine multiple conditions to make complex decisions. They're essential for filtering and multi-criteria checks.
The Big Three
AND
True only if BOTH conditions are true
OR
True if AT LEAST ONE condition is true
NOT
Reverses the result (True becomes False)
The Truth Table
A truth table shows all possible combinations of inputs and their results:
| A | B | A AND B | A OR B | NOT A |
|---|---|---|---|---|
| True | True | True | True | False |
| True | False | False | True | False |
| False | True | False | True | True |
| False | False | False | False | True |
Toggle the switches to see how current flows through AND vs OR gates!
Current flows only when the logic condition is met
Combining Operators (The Mega-Expression)
Real programs often combine multiple operators in complex expressions. Understanding precedence is crucial!
Complex Logic Example
Operator Precedence Order
When all three types appear together, this is the order in which they're evaluated:
- Arithmetic (calculations first)
- Relational (comparisons second)
- Logical (AND, OR, NOT last)
Click on the part of the expression that would be calculated FIRST, then SECOND, etc.
CSEC Exam Pitfalls
These common mistakes trip up many students on exams. Learn to avoid them!
The "Assignment vs. Equality" Confusion
Assignment (=)
SET X = 5 means "store the value 5 in variable X"
Comparison (=)
IF X = 5 means "check if X equals 5 (True/False)"
Remember: In pseudocode, "=" can mean assignment, but in conditions it means comparison. Context matters!
The Range Trap
WRONG: IF 18 < Age < 25
This doesn't work because the computer evaluates 18 < Age first, getting True or False, then tries to compare that to 25.
CORRECT: IF (Age > 18) AND (Age < 25)
Each comparison is evaluated separately, then combined with AND.
Knowledge Check: Operator Expert
Question 1: Calculation Quiz
What is the result of 15 MOD 4?
Question 2: Logic Challenge
If A = True and B = False, what is the result of (A AND B) OR (NOT B)?
Question 3: Short Answer
Explain a real-world scenario where a programmer would use the MOD operator.
