🧠 Operators: The Verbs of Programming
Hello future Computer Scientists! Welcome to the essential chapter on Operators. Don't worry if the term sounds complicated; operators are simply the symbols we use to perform actions, calculations, or make comparisons in our code. They are the 'verbs' that tell the computer what to do!
Understanding operators is crucial because they allow you to write complex equations, control program flow (like making decisions), and manage data. If you can master this chapter, you’ll be ready to write powerful algorithms! Let’s jump right in!
Section 1: Arithmetic Operators (Doing the Math)
Arithmetic operators are the symbols used for mathematical calculations. These will look very familiar, but we have two special friends—MOD and DIV—that are unique to programming.
Basic Arithmetic Operators
These are just like your calculator functions:
- + : Addition (e.g., 5 + 3 results in 8)
- - : Subtraction (e.g., 10 - 2 results in 8)
- * : Multiplication (e.g., 4 * 4 results in 16)
- / : Real Division (e.g., 10 / 3 results in 3.333...)
The Special Operators: DIV and MOD
These two operators are specifically designed for working only with whole numbers (integers). They help us find out exactly how many times one number fits into another, and what’s left over.
Analogy: Think about sharing 17 slices of pizza among 5 people.
- DIV (Integer Division)
- MOD (Modulus or Remainder)
DIV finds the whole number quotient (the result of division, ignoring any remainder).
Example: 17 DIV 5 = 3
(Each person gets 3 whole slices of pizza.)
MOD finds the remainder after division. It tells you what is left over.
Example: 17 MOD 5 = 2
(There are 2 slices left over.)
Did you know? MOD is super useful for checking if a number is odd or even! If X MOD 2 equals 0, the number X is even.
Section 2: Relational Operators (Making Comparisons)
Relational operators, also called Comparison operators, are used to compare two values. They ask a question about the relationship between two items.
Example: Is Variable A greater than Variable B?
The result of any relational operation is always a Boolean value—meaning the answer is either TRUE or FALSE.
List of Relational Operators
| Operator | Meaning | Example (Result) |
= or == |
Equal to | 5 == 5 (TRUE) |
<> or != |
Not equal to | 10 != 5 (TRUE) |
> |
Greater than | 7 > 3 (TRUE) |
< |
Less than | 1 < 0 (FALSE) |
>= |
Greater than or equal to | 8 >= 8 (TRUE) |
<= |
Less than or equal to | 4 <= 10 (TRUE) |
🚨 Common Mistake Alert!
In programming, we often use the single equals sign (=) for assignment (storing a value, e.g., A = 5). We use the double equals sign (==) or = (depending on the pseudo-code standard) for comparison (checking if two things are equal, e.g., A == 5).
Make sure you know which symbol your specific exam board uses for comparison! For this course, you should be familiar with both = and == for checking equality, and <> or != for inequality.
Section 3: Logical (Boolean) Operators (Making Decisions)
Logical operators allow us to combine multiple relational (TRUE/FALSE) conditions into one single, larger condition. They are essential for creating complex decisions in programs (like in IF statements).
The three main logical operators are AND, OR, and NOT.
1. AND Operator
The AND operator requires all conditions to be TRUE for the final result to be TRUE. If even one condition is FALSE, the whole statement is FALSE.
Analogy: To pass a driving test, you must pass the Theory test AND you must pass the Practical test. If you fail one, you fail the whole test.
| Condition A | Condition B | A AND B |
| TRUE | TRUE | TRUE |
| TRUE | FALSE | FALSE |
| FALSE | TRUE | FALSE |
| FALSE | FALSE | FALSE |
2. OR Operator
The OR operator requires only at least one condition to be TRUE for the final result to be TRUE. The only time an OR statement is FALSE is if all conditions are FALSE.
Analogy: To get into the cinema, you must have a ticket OR you must have an employee badge. If you have either one (or both), you get in.
| Condition A | Condition B | A OR B |
| TRUE | TRUE | TRUE |
| TRUE | FALSE | TRUE |
| FALSE | TRUE | TRUE |
| FALSE | FALSE | FALSE |
3. NOT Operator
The NOT operator simply reverses or negates the condition. It turns TRUE into FALSE, and FALSE into TRUE.
Example: If isRaining is TRUE, then NOT isRaining is FALSE.
| Condition A | NOT A |
| TRUE | FALSE |
| FALSE | TRUE |
Section 4: Operator Precedence (The Order of Operations)
What happens if you have a line of code with many different operators, like arithmetic, comparison, and logical operators all mixed together?
Just like in regular maths where you use BODMAS (Brackets, Orders, Division/Multiplication, Addition/Subtraction), computer programs follow a specific order called Operator Precedence.
If you don't follow this order, your program will calculate the answer incorrectly!
Precedence Hierarchy (Highest to Lowest)
The computer performs calculations in this specific sequence. Items higher up are done first:
- ( ) Brackets / Parentheses (Always calculated first to force priority)
- NOT (Logical Negation)
- Arithmetic Operators (* / DIV MOD + -)
- Relational/Comparison Operators (=, <>, <, >, <=, >=)
- AND (Logical Conjunction)
- OR (Logical Disjunction)
Step-by-Step Example of Precedence
Let’s evaluate this expression where X = 5 and Y = 10:
(X * 2 > Y) OR (Y - 5 == X)
- Step 1: Brackets ( ) - Calculate the expression inside the first bracket:
- Step 2: Brackets ( ) - Calculate the arithmetic inside the second bracket:
- Step 3: Relational Operators (inside brackets) - Evaluate comparisons:
- Step 4: OR Operator - Evaluate the final logical operator:
X * 2 (5 * 2) = 10.
Expression is now: (10 > Y) OR (Y - 5 == X)
Y - 5 (10 - 5) = 5.
Expression is now: (10 > Y) OR (5 == X)
First comparison: 10 > Y (10 > 10) results in FALSE.
Second comparison: 5 == X (5 == 5) results in TRUE.
Expression is now: FALSE OR TRUE
FALSE OR TRUE results in TRUE.
The final result of the whole expression is TRUE.
🧠 Memory Aid Trick!
Don't worry about memorising the full list immediately. Remember the flow:
1. Brackets first!
2. NOT is next (it’s a single flip, so it’s powerful).
3. Arithmetic (the core math)
4. Relational/Comparison (this results in TRUE/FALSE)
5. Logical (AND, then OR)
() to control the order if you are unsure. If brackets aren't used, the computer follows the hierarchy: NOT, then Arithmetic, then Comparison, then Logical (AND before OR).
You’ve covered the core building blocks of computer calculations! Keep practicing how DIV and MOD work, and try drawing out Truth Tables for the logical operators. Great work!