Operators are important parts in every programming language and it’s allow to perform operations on values and variables.
Using operators, we can easily add two numbers, compare values, or combine logic. In Mojo, operators are clean, readable, and similar to those found in Python and C-like languages.
Operators are an essential part, so read this topic carefully.
Simple example of operator:
fn main():
let a: Int = 10
let b: Int = 5
let sum: Int = a + b # Addition operator
print("The sum is:", sum)
Explanation of this simple example:
- let a: Int = 10 → This line is a variable “a” with the value 10.
- let b: Int = 5 → This is variable “b” with the value 5
- let sum: Int = a + b → It use the + (Addition operator) and prints the result on the screen.
Types of Operators in Mojo
- Arithmetic Operators
- Assignment Operators
- Comparison Operators
- Logical Operators
- Logical Operators
We all know that multiple types of Operators are available in programming languages, but different languages use them differently, and all have their unique syntaxes.
Mojo supports various Operators in multiple categories.
1) Arithmetic Operators
This Operator is used to perform only basic math operations like addition, subtraction, multiplication, etc.
| Operator | Meaning | Example |
|---|---|---|
| + | Addition | a + b |
| – | Subtraction | a – b |
| * | Multiplication | a * b |
| / | Division | a / b |
| % | Modulus | a % b |
Arithmetic Operator Example: Simple Bill Splitter
fn main():
let total_amount: Int = 350
let number_of_people: Int = 4
let split_amount: Int = total_amount / number_of_people
print("Each person should pay ₹", split_amount)
Output:
Each person should pay ₹87
In this example, we used the Division ( / ) operator for calculation. the code splits the total amount by people and prints the final amount.
2) Assignment Operators
This Operator assigns the values to the variables. Let’s see the following table:
| Operator | Meaning | Example |
|---|---|---|
| = | Assign | x = 10 |
| += | Add and assign | x += 5 |
| -= | Subtract and assign | x -= 3 |
| *= | Multiply and assign | x *= 2 |
| /= | Divide and assign | x /= 4 |
Fitness Tracker Points Example
fn main():
var points: Int = 0
points += 10 # walked 5k steps
points += 15 # did 30 min cycling
points -= 5 # skipped water intake
print("Total fitness points today:", points)
Output:
Total fitness points today: 20
In this code, we used an assignment operator to walk points, add and skip according to the steps.
c) Comparison Operators
Comparison Operator compares the values and gives the result in Boolean values (True or False).
| Operator | Meaning | Example |
|---|---|---|
| == | Equal to | a == b |
| != | Not equal to | a != b |
| > | Greater than | a > b |
| < | Less than | a < b |
| >= | Greater or equal | a >= b |
| <= | Less or equal | a <= b |
This table shows the multiple values comparisons using different signs.
Simple Example: Movie Rating Filter
fn main():
let rating: Float32 = 8.6
if rating >= 8.0:
print("Highly recommended movie!")
else:
print("Watch if you like the genre.")
Output:
Highly recommended movie!
This example shows the movie rating using the greater-than-or-equal sign. If the rating is more than 8.0, it will print the “Highly recommended movie!” message; otherwise print the message “Watch if you like the genre.”
4) Logical Operators
This Operator also gives me Boolean values but combines multiple Boolean conditions. There are multiple types of logical operators|:
| Operator | Meaning | Example |
|---|---|---|
| and | Logical AND | a > 5 and b < 10 |
| or | Logical OR | a == 0 or b == 1 |
| not | Logical NOT | not is_logged_in |
Simple Login Access Example
fn main():
let is_logged_in: Bool = true
let has_permission: Bool = true
if is_logged_in and has_permission:
print("Access granted to dashboard")
else:
print("Access denied")
Output:
Access granted to dashboard
5) Bitwise Operators (For Advanced Use)
Bitwise operators perform operations at the bit level, which means bitwise operators work on binary numbers (0s and 1s).
For example: The number 6 in binary is 0110 and the number 3 in binary is 0011.
Why use Bitwise Operators?
- These operators are very fast
- Useful in hardware-level programming
- Help in saving memory and performance
- Used in games, network, and math optimization
Types of Bitwise operators:
| Operator | Meaning | Example |
|---|---|---|
| & | AND | a & b |
| ` | ` | OR |
| ^ | XOR | a ^ b |
| ~ | NOT | ~a |
| << | Left shift | a << 2 |
| >> | Right shift | a >> 1 |
These are mostly important in critical-performance and hardware-level applications.
Exercise For You:
Create a Mini Budget Calculator App that can do the following terms:
- Takes income and expenses
- Calculates savings
- Gives alerts if savings are below a threshold
This example will use the:
- Arithmetic operators (+, -)
- Comparison operators (<)
- Logical operators (and, or)