What Are The Operators In Mojo?

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.

OperatorMeaningExample
+Additiona + b
Subtractiona – b
*Multiplicationa * b
/Divisiona / b
%Modulusa % 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:

OperatorMeaningExample
=Assignx = 10
+=Add and assignx += 5
-=Subtract and assignx -= 3
*=Multiply and assignx *= 2
/=Divide and assignx /= 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).

OperatorMeaningExample
==Equal toa == b
!=Not equal toa != b
>Greater thana > b
<Less thana < b
>=Greater or equala >= b
<=Less or equala <= 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|:

OperatorMeaningExample
andLogical ANDa > 5 and b < 10
orLogical ORa == 0 or b == 1
notLogical NOTnot 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:

OperatorMeaningExample
&ANDa & b
``OR
^XORa ^ b
~NOT~a
<<Left shifta << 2
>>Right shifta >> 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)

Also Learn this Topics:

Leave a Comment