Understanding Operators in SheerPower
Operators are the symbols that perform operations on variables
and values. They are the building blocks for doing math, making
comparisons, and creating logic in your programs. This tutorial
is broken down into three parts to make learning easy and fun.
Part 1: The Basics - Mathematical Operators
These are the operators you use for basic arithmetic.
+ (Addition)
- (Subtraction)
* (Multiplication)
/ (Division)
^ (Exponentiation, e.g., 2^3 is 8)
++ (Increment, adds 1 to a variable, e.g., counter++)
Example: Calculating a Shopping Bill
apple_price = 0.50
banana_price = 0.30
num_apples = 4
num_bananas = 6
! Calculate the cost for each fruit
total_apple_cost = apple_price * num_apples
total_banana_cost = banana_price * num_bananas
! Calculate the subtotal
subtotal = total_apple_cost + total_banana_cost
print "Subtotal: $"; subtotal
! Add one more apple
num_apples++
print "Number of apples is now: "; num_apples
Part 2: Making Decisions - Relational and Logical Operators
These operators are used to compare values and combine conditions,
which is essential for making decisions in your code.
Relational Operators (for comparing values)
= (Equal to)
<> (Not equal to)
> (Greater than)
< (Less than)
>= (Greater than or equal to)
<= (Less than or equal to)
Logical Operators (for combining true/false conditions)
AND (Returns true if BOTH conditions are true)
OR (Returns true if AT LEAST ONE condition is true)
NOT (Reverses the condition, true becomes false)
Example: Qualifying for a Discount
is_member? = true
purchase_total = 75.00
! Check if the customer gets a discount
is_eligible? = is_member? OR purchase_total > 100.00
print "Is eligible for discount? "; is_eligible?
Advanced Logical Operators
SheerPower also supports more advanced logical operators. Their
behavior is easiest to understand using "Truth Tables."
Truth Table for XOR, IMP, and EQV
X | Y |
X XOR Y |
X IMP Y |
X EQV Y |
TRUE | TRUE |
FALSE |
TRUE |
TRUE |
TRUE | FALSE |
TRUE |
FALSE |
FALSE |
FALSE | TRUE |
TRUE |
TRUE |
FALSE |
FALSE | FALSE |
FALSE |
TRUE |
TRUE |
Part 3: How It All Works - Order of Operations
SheerPower, like all programming languages, follows a specific
order when evaluating expressions. This is called
operator precedence. Understanding this order is
crucial for getting the correct results.
Order of Operations
- Parentheses `()`
Expressions inside parentheses are always evaluated first.
- Exponentiation `^`
Powers are calculated next.
- Multiplication `*` and Division `/`
These are evaluated from left to right.
- Addition `+` and Subtraction `-`
These are evaluated next, from left to right.
- Relational & Logical Operators
Comparisons and logical operations are done last.
Example: In the expression `4 + 5 * 2`, the
multiplication `5 * 2` happens first, resulting in `10`. Then the
addition `4 + 10` is performed, giving a final result of `14`.
To change this, you would use parentheses: `(4 + 5) * 2`, which
evaluates to `18`.
Summary: You've learned about the three main
types of operators in SheerPower: mathematical for calculations,
relational and logical for decision-making, and the all-important
order of operations that ensures your expressions are evaluated
correctly.