🧠 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.

  1. DIV (Integer Division)
  2. 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.)

  3. MOD (Modulus or Remainder)
  4. 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.

Key Takeaway (Arithmetic): You need to know +, -, *, /, and especially understand the difference between DIV (whole answer) and MOD (the remainder).

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.

Key Takeaway (Relational): These operators compare two things and always result in either TRUE or FALSE.

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.

Truth Table for AND
Condition A Condition B A AND B
TRUETRUETRUE
TRUEFALSEFALSE
FALSETRUEFALSE
FALSEFALSEFALSE
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.

Truth Table for OR
Condition A Condition B A OR B
TRUETRUETRUE
TRUEFALSETRUE
FALSETRUETRUE
FALSEFALSEFALSE
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.

Truth Table for NOT
Condition A NOT A
TRUEFALSE
FALSETRUE
Key Takeaway (Logical): AND is strict (needs all TRUE). OR is flexible (needs at least one TRUE). NOT flips the result.

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:

  1. ( ) Brackets / Parentheses (Always calculated first to force priority)
  2. NOT (Logical Negation)
  3. Arithmetic Operators (* / DIV MOD + -)
  4. Relational/Comparison Operators (=, <>, <, >, <=, >=)
  5. AND (Logical Conjunction)
  6. 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)

  1. Step 1: Brackets ( ) - Calculate the expression inside the first bracket:
  2. X * 2 (5 * 2) = 10.
    Expression is now: (10 > Y) OR (Y - 5 == X)

  3. Step 2: Brackets ( ) - Calculate the arithmetic inside the second bracket:
  4. Y - 5 (10 - 5) = 5.
    Expression is now: (10 > Y) OR (5 == X)

  5. Step 3: Relational Operators (inside brackets) - Evaluate comparisons:
  6. First comparison: 10 > Y (10 > 10) results in FALSE.
    Second comparison: 5 == X (5 == 5) results in TRUE.
    Expression is now: FALSE OR TRUE

  7. Step 4: OR Operator - Evaluate the final logical operator:
  8. 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)

Key Takeaway (Precedence): Always use parentheses () 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!