Operators¶
Operators perform operations on values and variables.
Arithmetic Operators¶
Basic Arithmetic¶
| Operator | Description | Example | Result |
|---|---|---|---|
+ |
Addition | 5 + 3 |
8 |
- |
Subtraction | 5 - 3 |
2 |
* |
Multiplication | 5 * 3 |
15 |
/ |
Division | 10 / 2 |
5 |
% |
Modulo (remainder) | 10 % 3 |
1 |
let a = 10;
let b = 3;
print a + b; // 13
print a - b; // 7
print a * b; // 30
print a / b; // 3.333...
print a % b; // 1
Unary Minus¶
Comparison Operators¶
| Operator | Description | Example | Result |
|---|---|---|---|
== |
Equal to | 5 == 5 |
true |
!= |
Not equal to | 5 != 3 |
true |
< |
Less than | 3 < 5 |
true |
> |
Greater than | 5 > 3 |
true |
<= |
Less than or equal | 5 <= 5 |
true |
>= |
Greater than or equal | 5 >= 3 |
true |
let x = 10;
let y = 20;
print x == y; // false
print x != y; // true
print x < y; // true
print x > y; // false
print x <= 10; // true
print y >= 20; // true
String Comparison¶
let name1 = "Alice";
let name2 = "Bob";
print name1 == name2; // false
print name1 != name2; // true
Logical Operators¶
| Operator | Description | Example | Result |
|---|---|---|---|
&& |
Logical AND | true && false |
false |
\|\| |
Logical OR | true \|\| false |
true |
! |
Logical NOT | !true |
false |
let a = true;
let b = false;
print a && b; // false
print a || b; // true
print !a; // false
print !b; // true
Short-Circuit Evaluation¶
let x = 10;
// && stops if first is false
if x > 5 && x < 20 {
print "x is between 5 and 20";
}
// || stops if first is true
if x < 5 || x > 0 {
print "This prints because x > 0 is true";
}
Bitwise Operators¶
| Operator | Description | Example | Result |
|---|---|---|---|
& |
Bitwise AND | 12 & 10 |
8 |
\| |
Bitwise OR | 12 \| 10 |
14 |
^ |
Bitwise XOR | 12 ^ 10 |
6 |
~ |
Bitwise NOT | ~5 |
-6 |
<< |
Left shift | 5 << 1 |
10 |
>> |
Right shift | 10 >> 1 |
5 |
let a = 12; // 1100 in binary
let b = 10; // 1010 in binary
print a & b; // 8 (1000)
print a | b; // 14 (1110)
print a ^ b; // 6 (0110)
print ~a; // -13
print a << 1; // 24 (multiply by 2)
print a >> 1; // 6 (divide by 2)
String Concatenation¶
Use + to concatenate strings:
let first = "Hello";
let second = "World";
let message = first + " " + second;
print message; // Hello World
Cannot concatenate string with other types directly:
let text = "Number: ";
let num = 42;
// Wrong
let result = text + num; // Error
// Correct - convert to string first
let result = text + str(num);
print result; // Number: 42
// Or use interpolation
let result = "Number: #{num}";
print result; // Number: 42
Assignment Operator¶
The = operator assigns values:
Operator Precedence¶
Operators are evaluated in this order (highest to lowest):
- Unary:
!,-,~ - Multiplicative:
*,/,% - Additive:
+,- - Shift:
<<,>> - Comparison:
<,>,<=,>= - Equality:
==,!= - Bitwise AND:
& - Bitwise XOR:
^ - Bitwise OR:
| - Logical AND:
&& - Logical OR:
||
// Without parentheses
let result = 2 + 3 * 4;
print result; // 14 (multiplication first)
// With parentheses
let result = (2 + 3) * 4;
print result; // 20 (addition first)
Using Parentheses¶
Use parentheses to override precedence:
let a = 10;
let b = 5;
let c = 2;
// Natural precedence
print a + b * c; // 20 (5*2 + 10)
// Override with parentheses
print (a + b) * c; // 30 ((10+5)*2)
Compound Conditions¶
let age = 25;
let has_license = true;
// Multiple conditions with AND
if age >= 18 && has_license == true {
print "Can drive";
}
// Multiple conditions with OR
if age < 18 || has_license == false {
print "Cannot drive";
} else {
print "Can drive";
}
// Complex conditions
if (age >= 16 && age < 18) || has_license == false {
print "Restricted driving";
}
Truthiness¶
Non-boolean values are evaluated in boolean contexts:
// Truthy values
if 1 { print "Numbers != 0 are truthy"; }
if "hello" { print "Non-empty strings are truthy"; }
if [1,2,3] { print "Arrays are truthy"; }
// Falsy values
if 0 { } else { print "0 is falsy"; }
if "" { } else { print "Empty string is falsy"; }
Common Patterns¶
Range Check¶
let score = 85;
if score >= 90 && score <= 100 {
print "A";
} else if score >= 80 && score < 90 {
print "B";
}
Null Check Pattern¶
Toggle Pattern¶
Even/Odd Check¶
Increment/Decrement¶
Multiply/Divide by Powers of 2¶
let n = 8;
// Faster than n * 2
let doubled = n << 1; // 16
// Faster than n / 2
let halved = n >> 1; // 4
Mathematical Operations¶
// Calculate average
let sum = 0;
let numbers = [10, 20, 30, 40, 50];
for i in 0..len(numbers) {
sum = sum + numbers[i];
}
let average = sum / len(numbers);
print average; // 30
// Calculate percentage
let score = 85;
let total = 100;
let percentage = (score / total) * 100;
print percentage; // 85
// Compound interest formula
let principal = 1000;
let rate = 0.05;
let time = 2;
let amount = principal * pow(1 + rate, time);
print amount; // 1102.5
Operator Chaining¶
let a = 5;
let b = 10;
let c = 15;
// Chain comparisons using AND
if a < b && b < c {
print "a < b < c";
}
// Multiple arithmetic operations
let result = a + b * c - d / e;
Common Mistakes¶
Confusing Assignment and Equality¶
let x = 10;
// Wrong - assignment, not comparison
if x = 5 { // This assigns 5 to x!
print "Oops";
}
// Correct - comparison
if x == 5 {
print "x is 5";
}
Integer Division¶
Mixing Types¶
let x = "10";
let y = 5;
// Wrong
print x + y; // Error: can't add string and number
// Correct
print int(x) + y; // 15
print x + str(y); // "105"
Best Practices¶
-
Use parentheses for clarity
-
Avoid complex expressions
-
Use named constants
Next Steps¶
- Learn about control flow
- Explore functions
- See data types