if Conditional Statement in Mojo Programming Language: Programming in Mojo — Part VI
Mojo Programming Language combines the usability of Python with the performance of C, C++ and Rust. Mojo Programming Language utilizes next-generation compiler technologies with features like caching, multithreading and cloud distribution technologies. Further, Auto-Tuning and compile-time Metaprogramming features allows to write code for several hardware. The advantages of Mojo Programming Language are Usability & Programmability, best performance, Interoperability and Extensibility.
In this Blog, Programming in Mojo is done using Mojo Playground and we are going to see a simple implementation of if Conditional Statement.
Mojo supports all of Python’s control-flow syntax such as if conditions and for loops. Mojo Programming Language supports Python Programming Language if Conditional Statement syntax and for Loop Statement syntax.
Since, Mojo Programming Language is still in development some features of Python Programming Language are not yet implemented in Mojo, but all the missing Python features will be implemented soon as mentioned by Modular-Mojo Development Team. But Mojo Programming Language already consists of many features and capabilities beyond Python Programming Language.
In this Blog we are going to discuss the following conditional statements,
1. if Conditional Statement
2. if…else Conditional Statement
3. if…elif…else Conditional Statement
4. Nested if Conditional Statement
I. if Conditional Statement in Mojo Programming Language:
The if statement is used for conditional execution. If the condition / expression evaluates to True, it executes a statement or a block of statements inside the body of if statement.
If the condition / expression evaluates to False, it skips the statement or a block of statements inside the body of if statement and executes the statement after the body of if statement.
Like Python, Mojo Programming Language uses line breaks and indentation to define code blocks and not the curly braces. Mojo Programming Language uses indentation to identify a block, so the block under an if conditional statement will be identified as below,
Example:
II. if…else Conditional Statement in Mojo Programming Language:
The if…else statement is used for conditional execution. If the condition / expression evaluates to True, it executes a statement or a block of statements inside the body of if statement and skips the statement or a block of statements inside the body of else statement.
If the condition / expression evaluates to False, it skips the statement or a block of statements inside the body of if statement and executes a statement or a block of statements inside the body of else statement.
Example:
III. if…elif…else Conditional Statement in Mojo Programming Language:
The keyword elif is short for else if. The if…elif…else statement is used for conditional execution of statements among several many conditions and the if conditional statements are evaluated from Top to Down and as soon as if one of the condition / expression of any of the if statement is True, statement or block of statements inside the body of the if statement is executed and rest of the statements of if…elif…else Conditional Statement are skipped.
If none of the condition / expression evaluates to True, then the statement or a block of statements inside the body of else statement are executed.
Note that there can be zero or more number of elif parts and the else part is optional.
Example:
IV. Nested if Conditional Statement in Mojo Programming Language:
We can use an if conditional statement inside of an if conditional statement, this is called Nested if conditional statement. The condition / expression of the inner if statement will be evaluated only if the condition / expression of the outer if statement evaluates to True.
According to the requirements, we can nest as many layers of if statements in Nested if Conditional Statement, also to the inner if conditional statement we can add else and elif statements, and also inside the outer else or elif statement we can insert if statement to it.
Example:
We had seen about if Conditional Statement in Mojo Programming Language. Programming in Mojo and Mojo Programming concepts will be discussed further.
Thankyou for reading this blog on if Conditional Statement in Mojo Programming Language: Programming in Mojo — Part VI.
Reference: