Conditional Statements in Mojo: if, if-else, elif

conditional statements in Mojo

Learn conditional statements in the Mojo programming language for making decisions within a program. The blog will cover if, if-else, and else-if statements with real-world examples, engaging visuals, and easy code-snippets.

What are Conditional Statements?

Conditional statements, also known as decision statements that decide what actions to take based on the condition. This allows the program to respond according to different situations. Conditional statements make your program more interactive and user-friendly, as they change its output based on the condition.

For example, we can instruct the program to output something when a condition is true, and a different output when it’s false. If none of the conditions are met, we can offer an alternative action.

Mojo uses the same syntax as Python for conditional statements, making it easy for those familiar with Python conditional statements to understand and use.

If Statement in Mojo

In Mojo, the if statement is the simplest way to make decisions in your code. It executes a specific block of code when a condition is true. Similar to asking, “Is this condition true? If the condition is true, the code within the “if” block will be executed. If the condition isn’t met, The program will skip the code block in the if condition and move to the rest of the code.

Real-World Example of If Statement

A simple way to understand if statements are to relate them to a real-world scenario, such as a simple Alarm clock that will go off if it’s a weekday.

  • if the day is a weekday -> alarm goes off
real world example of if statement in Mojo programming language

Flow Chart

Flow Chart for If statements in Mojo programming language

Syntax

if condition:
    # Run this code if the condition is true.
    # Second Statement
    # third statement 

Example Code

  • A variable called temperature is created and set to the value 30. This represents a specific temperature
  • An if statement begins. It checks whether the value of temperature is equal to 30.
  • If this condition is true, the code block that follows (indented) will be executed.
  • This line is executed only if the condition in the if statement is true (i.e. when the temperature is 30).
  • It outputs the string “It’s a warm day!” to the console, letting the user know it’s warm.
temperature = 30

if temperature == 30:
    print("It's a warm day!")

Output

It's a warm day!

If else in Mojo

In Mojo, just like in C, C++ and Python, the if statement lets you run a block of code when a condition is true. But what if you want to take action when that condition is false? This is where the else statement steps in. You can follow an if with an else to specify what should happen when the condition is not met. It’s like giving your program two choices based on the condition: one action if it’s true and another if it’s false. In an if-else structure, one of the blocks will always run, ensuring your program has a clear path to follow in different situations.

Real-world Example of If-else Statement

Take a situation where you need to reach a destination, if the distance is short then you would walk, and if not then you would take a vehicle ( bike, car, etc. ) giving you a similar situation that has two different outcomes depending on the scenario.

real world example for using if else statements in Mojo programming language.

Flow Chart

Flow chart for if-else statements in Mojo programming language

Syntax

if condition:
    # Block of code that runs when the if condition is satisfied
    statement1
    statement2
else:
    # Code block to run when the if condition is false.
    statement3
    statement4

Example Code

  • first declares a variable named temperature and assigns it a value of 30. This value represents the current temperature.
  • Then, we use the if statement to check whether the value of temperature is greater than 20.
  • If this condition evaluates to true, the code block that follows will be executed.
  • After that the if block. If the condition (temperature > 20) is true, this line will execute and print the message: “It’s warm outside.”
  • Then the else block, which provides an alternative action.
  • Then the else block. If the if condition is false (meaning the temperature is 20 or less), this line will execute and print the message: “It’s cold outside.”
temperature = 30

if temperature > 20:
    print("It's warm outside.")
else:
    print("It's cold outside.")

Output

It's warm outside.

elif in Mojo

Similar to how the elif statement in Python, elif in Mojo allows your program to choose between two actions based on a condition, the elif statement in Mojo lets you add more conditions to check after the initial if statement. This is useful when you have several conditions to consider, and only one of them can be true at a time. When you have multiple elif statements in your program, Mojo checks them from top to bottom to see which condition is true. As soon as it finds a true condition, it stops checking the rest and moves directly to the end of the else or elif.

The else part is optional here and will run when none of the previous conditions are true. The elif statements create what’s known as an elif ladder, as it functions like a ladder where you check if the current condition is true at each step. This is why the flowchart for else-if resembles a ladder.

Real-world Example of elif Statement

A simple example that you can relate to is when going out you choose the top you are going to wear according to the weather such as (a T-shirt, jacket, long coat, rain coat).

real world example of elif statements in Mojo programming language

Flow Chart

Flow chart for using elif statements in Mojo programming language

Syntax

if condition1:
    # Code block that runs when condition1 is true.
elif condition2:
    # Code block that executes when condition2 is true.
elif condition3:
    # Code block for condition3 being true
else:
    # Code block when none of the above conditions are true

Example Code

  • A variable named temperature is created and assigned the value 25.
  • The program verifies whether the temperature is below 0. If this condition holds true, it will run the code in the following line.
  • When the if condition is true, it prints “It’s freezing!”.
  •  If the first condition is false, the program checks if the temperature is less than 20. If true, it proceeds to the next line.
  • If the elif condition is true, it prints “It’s cool.”.
  •  If the second condition is also false, it checks if the temperature is less than 30.
  • If the second elif condition is true, it prints “It’s warm.”
  •  If none of the previous conditions were true, it executes this final block and prints “It’s hot!”..
temperature = 25

if temperature < 0:
    print("It's freezing!")
elif temperature < 20:
    print("It's cool.")
elif temperature < 30:
    print("It's warm.")
else:
    print("It's hot!")

Output

It's warm.

Conclusion

In this article, you learned about if, if-else, and elif conditional statements, along with their syntax, flowchart, real-world examples, engaging visuals, and code snippets specially designed for beginner programmers which is the aim of Syntax Scenarios. Conditional statements in Mojo allow you to make your programs more interactive and user-friendly and are the building blocks to developing large-scale applications.

For more detailed info rearding conditionl statements, you can visit the official guide on Control flow by Modular.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top