Welcome to the World of Operators!

In this chapter, we are going to look at the "tools" that allow computers to actually do work. In Computer Science, operators are special symbols that tell the computer to perform specific mathematical, comparison, or logical tasks.

Think of operators like the buttons on a calculator or the signs on a road. They take pieces of data (called operands) and produce a result. By the end of these notes, you’ll be able to use these tools to build clever programs that can calculate, compare, and make decisions!

1. Arithmetic Operators

These are used to perform calculations. You will recognize many of these from your maths lessons, but there are a few "special" ones that programmers use to handle numbers in specific ways.

Basic Arithmetic

  • Addition (+): Adds two values together. \( 5 + 3 = 8 \)
  • Subtraction (-): Subtracts one value from another. \( 10 - 4 = 6 \)
  • Multiplication (*): Multiplies two values. Note: We use the asterisk symbol, not an 'x'. \( 4 * 3 = 12 \)
  • Division (/): Divides one value by another and gives a decimal answer. \( 7 / 2 = 3.5 \)

The "Special" Three (DIV, MOD, and Exponentiation)

Don't worry if these seem tricky at first! They are just different ways of looking at division and powers.

Integer Division (DIV or //): This tells you how many whole times a number goes into another. It ignores the remainder completely.
Analogy: If you have 7 cookies and give them to 2 friends, they get 3 whole cookies each. The "0.5" or "1 left over" is ignored.
Example: \( 7 // 2 = 3 \)

Modulus (MOD or %): This gives you only the remainder left over after a division.
Analogy: Back to the 7 cookies. After you give 3 each to your 2 friends, you have 1 cookie left over. That 1 is the Modulus!
Example: \( 7 \% 2 = 1 \)

Exponentiation (^ or **): This raises a number to the power of another.
Example: \( 2^3 \) (which is \( 2 * 2 * 2 \)) \( = 8 \)

Quick Review: DIV vs MOD

Imagine the sum: \( 17 / 5 \)
- Normal division = \( 3.4 \)
- DIV = 3 (The whole number)
- MOD = 2 (The remainder, because \( 5 * 3 = 15 \), and \( 17 - 15 = 2 \))

Key Takeaway: Arithmetic operators allow your program to handle all the "maths" behind the scenes, from calculating game scores to splitting a restaurant bill.


2. Relational Operators

Relational operators are used to compare two values. The result of a comparison is always a Boolean value: either True or False.

  • Equal to (==): Checks if two values are exactly the same. (Warning: Use two equals signs! A single = is for assigning a value to a variable).
  • Not equal to (!=): Checks if two values are different.
  • Greater than (>): Checks if the left value is bigger.
  • Less than (<): Checks if the right value is bigger.
  • Greater than or equal to (>=): Checks if the left value is bigger or the same.
  • Less than or equal to (<=): Checks if the right value is bigger or the same.
Common Mistake Alert!

Many students use = when they mean ==.
- Score = 10 (This sets the score to 10).
- If Score == 10 (This asks "is the score 10?").

Key Takeaway: Relational operators are the heart of "Selection" (If-Statements). They allow the computer to ask questions like "Is the user's password correct?" or "Does the player have enough gold?"


3. Logical Operators (AND, OR, NOT)

Sometimes, one comparison isn't enough. We use Logical Operators to combine multiple comparisons together.

The Big Three:

1. AND: Both sides must be True for the whole thing to be True.
Example: To go outside, you must have (Shoes ON) AND (Coat ON). If you are missing one, the result is False!

2. OR: Only one side needs to be True for the whole thing to be True.
Example: You can have dessert if you (Eat your vegetables) OR (It is your birthday). If either is True, you get the cake!

3. NOT: This simply reverses the result. It turns True into False, and False into True.
Example: NOT (Hungry) means you are full.

Truth Tables

You may be asked to complete a Truth Table for these operators. Here is a simple look at how they work with two inputs (A and B):

The AND Truth Table

\( A = False, B = False \rightarrow Result: False \)
\( A = True, B = False \rightarrow Result: False \)
\( A = False, B = True \rightarrow Result: False \)
\( A = True, B = True \rightarrow Result: True \)

The OR Truth Table

\( A = False, B = False \rightarrow Result: False \)
\( A = True, B = False \rightarrow Result: True \)
\( A = False, B = True \rightarrow Result: True \)
\( A = True, B = True \rightarrow Result: True \)

Did you know? You can combine up to three inputs in your exam! For example: (A AND B) OR C. Just work through the brackets first, exactly like you do in maths.

Key Takeaway: Logical operators allow for complex decision-making. They let you check multiple conditions at once to make your programs more powerful.


Summary Checklist

Before you move on, make sure you can answer these:
- Do I know the difference between /, DIV, and MOD?
- Can I list all 6 Relational Operators?
- Can I explain why AND requires both conditions to be met, but OR only needs one?
- Do I remember to use == for comparisons instead of =?

Don't worry if you have to look back at the cookie analogy a few times—MOD and DIV take practice to master!