Lesson 5 • Beginner

Operators

20 minutes
Foundation

🔢 What are Operators?

Operators are special symbols that perform operations on values and variables. They're the building blocks for calculations, comparisons, and logical decisions in your programs.

There are three main categories of operators:

  • Arithmetic Operators - Perform mathematical calculations
  • Comparison Operators - Compare values and return true/false
  • Logical Operators - Combine conditions and boolean values

Visual: Operator Categories

🔢
Arithmetic
+ - * / mod
Math operations
⚖️
Comparison
== != < > <= >=
Compare values
🔗
Logical
AND OR NOT
Combine conditions

➕ Arithmetic Operators

Use these operators to perform mathematical calculations:

Operator Name Example Result
+ Addition 5 + 3 8
- Subtraction 10 - 4 6
* Multiplication 6 * 7 42
/ Division 20 / 4 5
mod Modulus (Remainder) 17 mod 5 2
Example: Basic Arithmetic
Algorithm ArithmeticOperators

var a = 10
var b = 3

Print "a =", a
Print "b =", b
Print ""

Print "Addition: a + b =", a + b
Print "Subtraction: a - b =", a - b
Print "Multiplication: a * b =", a * b
Print "Division: a / b =", a / b
Print "Modulus: a mod b =", a mod b

Endalgorithm
Output
a = 10 b = 3 Addition: a + b = 13 Subtraction: a - b = 7 Multiplication: a * b = 30 Division: a / b = 3.333333 Modulus: a mod b = 1

Understanding Modulus (mod)

The modulus operator returns the remainder after division. It's incredibly useful for:

  • Checking if a number is even or odd (n mod 2)
  • Cycling through ranges (array indices, days of week)
  • Finding patterns in numbers
Example: Even or Odd Checker
Algorithm EvenOddChecker

var number = Input "Enter a number:"
var remainder = number mod 2

Print ""
Print "Number:", number
Print "Remainder when divided by 2:", remainder

Endalgorithm

⚖️ Comparison Operators

Comparison operators compare two values and return true or false:

Operator Name Example Result
== Equal to 5 == 5 true
!= Not equal to 5 != 3 true
> Greater than 10 > 5 true
< Less than 3 < 8 true
>= Greater than or equal 5 >= 5 true
<= Less than or equal 4 <= 7 true
Example: Comparison Operations
Algorithm ComparisonExample

var age = 18
var votingAge = 18

Print "Age:", age
Print "Voting age:", votingAge
Print ""

Print "age == votingAge:", age == votingAge
Print "age >= votingAge:", age >= votingAge
Print "age < votingAge:", age < votingAge
Print "age != 21:", age != 21

Endalgorithm
Output
Age: 18 Voting age: 18 age == votingAge: true age >= votingAge: true age < votingAge: false age != 21: true

🔗 Logical Operators

Logical operators combine multiple conditions:

Operator Name Description Example
and AND True if BOTH conditions are true true and true → true
or OR True if AT LEAST ONE condition is true true or false → true
not NOT Reverses the boolean value not true → false

AND Operator

Both conditions must be true:

var hasTicket = true
var isOldEnough = true
var canEnter = hasTicket and isOldEnough  # true

OR Operator

At least one condition must be true:

var isWeekend = true
var isHoliday = false
var canRelax = isWeekend or isHoliday  # true

NOT Operator

Reverses true/false:

var isRaining = true
var isSunny = not isRaining  # false
Example: Logical Operators
Algorithm LogicalOperators

var age = 20
var hasLicense = true
var hasInsurance = true

Print "Age:", age
Print "Has license:", hasLicense
Print "Has insurance:", hasInsurance
Print ""

# AND: Both conditions must be true
var canDriveLegally = age >= 18 and hasLicense
Print "Can drive legally:", canDriveLegally

# Multiple AND conditions
var canRentCar = age >= 21 and hasLicense and hasInsurance
Print "Can rent car:", canRentCar

# OR: At least one condition must be true
var qualifiesForDiscount = age < 18 or age >= 65
Print "Qualifies for discount:", qualifiesForDiscount

# NOT: Reverse boolean
var needsTraining = not hasLicense
Print "Needs training:", needsTraining

Endalgorithm

📊 Operator Precedence

When multiple operators appear in an expression, they're evaluated in this order:

  1. Parentheses - ()
  2. NOT - not
  3. Multiplication, Division, Modulus - *, /, mod
  4. Addition, Subtraction - +, -
  5. Comparison - ==, !=, <, >, <=, >=
  6. AND - and
  7. OR - or
Example: Operator Precedence
Algorithm Precedence

# Without parentheses
var result1 = 2 + 3 * 4  # 2 + 12 = 14
Print "2 + 3 * 4 =", result1

# With parentheses
var result2 = (2 + 3) * 4  # 5 * 4 = 20
Print "(2 + 3) * 4 =", result2

# Complex expression
var a = 10
var b = 5
var c = 2

var result3 = a + b * c  # 10 + (5 * 2) = 20
Print "a + b * c =", result3

var result4 = (a + b) * c  # (10 + 5) * 2 = 30
Print "(a + b) * c =", result4

Endalgorithm
Best Practice
When in doubt, use parentheses! They make your code clearer and prevent mistakes. (a + b) * c is easier to understand than a + b * c even if they give different results.

Real-World Examples

Example: Sales Tax Calculator
Algorithm SalesTaxCalculator

Print "=== Sales Tax Calculator ==="
Print ""

var price = Input "Enter item price:"
var taxRate = 0.08  # 8% tax

# Calculate tax and total
var taxAmount = price * taxRate
var total = price + taxAmount

# Calculate savings with discount
var discount = 0.10  # 10% discount
var discountAmount = price * discount
var discountedPrice = price - discountAmount
var discountedTax = discountedPrice * taxRate
var discountedTotal = discountedPrice + discountedTax

Print ""
Print "Regular Price: $", price
Print "Tax (8%): $", taxAmount
Print "Total: $", total
Print ""
Print "With 10% Discount:"
Print "Discounted Price: $", discountedPrice
Print "Tax: $", discountedTax
Print "Total: $", discountedTotal
Print "You save: $", total - discountedTotal

Endalgorithm
Example: Grade Eligibility Checker
Algorithm GradeEligibility

var score = Input "Enter your test score (0-100):"
var attendance = Input "Enter attendance percentage:"

# Check pass/fail
var isPassing = score >= 60
var hasGoodAttendance = attendance >= 75

# Check eligibility for honors
var qualifiesForHonors = score >= 90 and attendance >= 90

# Check if needs extra help
var needsHelp = score < 70 or attendance < 75

Print ""
Print "=== GRADE REPORT ==="
Print "Score:", score
Print "Attendance:", attendance, "%"
Print ""
Print "Passing:", isPassing
Print "Good attendance:", hasGoodAttendance
Print "Qualifies for honors:", qualifiesForHonors
Print "Needs extra help:", needsHelp

Endalgorithm

📋 Quick Reference

Category Operators Example
Arithmetic + - * / mod 10 + 5
Comparison == != < > <= >= age >= 18
Logical and or not a and b

Key Takeaways

  • ✅ Arithmetic operators perform math: +, -, *, /, mod
  • ✅ Comparison operators compare values and return true/false
  • ✅ Logical operators combine conditions: and, or, not
  • mod returns the remainder (useful for even/odd checks)
  • ✅ Use parentheses to control order of operations
  • ✅ Operators are essential for calculations and decision-making

💪 Practice Exercises

Try These Exercises

Exercise 1: BMI Calculator
Calculate Body Mass Index using the formula: BMI = weight (kg) / (height (m) × height (m)). Get weight and height from user.
Exercise 2: Leap Year Checker
A year is a leap year if it's divisible by 4. Use mod operator to check if a year is a leap year.
Exercise 3: Movie Ticket Pricer
Calculate ticket price: Children (<12) pay $8, Adults pay $12, Seniors (>=65) pay $9. Use comparison operators.
Exercise 4: Password Strength
Check if a password is strong: length >= 8 AND has numbers. Use logical operators to combine conditions.
Well Done!
You now know how to perform calculations and make comparisons! In the next lesson, we'll learn about comments and documentation to make your code easier to understand.