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:
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 |
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
The modulus operator returns the remainder after division. It's incredibly useful for:
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 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 |
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
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 |
Both conditions must be true:
var hasTicket = true
var isOldEnough = true
var canEnter = hasTicket and isOldEnough # true
At least one condition must be true:
var isWeekend = true
var isHoliday = false
var canRelax = isWeekend or isHoliday # true
Reverses true/false:
var isRaining = true
var isSunny = not isRaining # false
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
When multiple operators appear in an expression, they're evaluated in this order:
()
not
*
, /
, mod
+
, -
==
, !=
, <
, >
, <=
, >=
and
or
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
(a + b) * c
is easier to understand than a + b * c
even if they give different results.
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
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
Category | Operators | Example |
---|---|---|
Arithmetic | + - * / mod |
10 + 5 |
Comparison | == != < > <= >= |
age >= 18 |
Logical | and or not |
a and b |
+
, -
, *
, /
, mod
and
, or
, not
mod
returns the remainder (useful for even/odd checks)