Nim Provides the following control flow execution.
- Simple if conditional block
- If else blocks
- elif alias else if blocks NIM language has no process for statements inside if or if-else blocks
Code statements inside if and else are indented with 2 or 4 spaces.
Nim If the condition
A simple if condition
is used to execute a code block statement if the conditional expression is true.
The conditional expression always results in the bool
type and the possible values - true
and false
only.
Here is the syntax of a simple if statement.
if conditional-expression:
//if code block executed
Here is an example
let first = 1 second = 2 c = 999
if first == second : echo “Equal "
Nim if else conditional statements
if-else conditional statements are used to execute statements based on conditional expressions.
Syntax
if conditional expression
if code block executed
else
else code block executed
if conditional expression
is true
, if code block executed, else code block executed.
There are no braces like in other programming languages. Code blocks inside if and else are indented with two spaces.
let
first =0
second = 0
if first < 0:
echo "Negative Number"
else:
echo "Positive Number"
Nim if else if statements example
if else if
is to execute multiple blocks of code based on multiple conditional expression values.
The conditional expression always results in the bool type and the possible values - true and false only.
if else
statements are used to execute multiple conditional statements based on true and false values.
else if can be replaced with elif
keyword.
Here is the syntax of the if else if else statement.
if condition1 :
// statements executes if condition1 is true
elif condition2:
// statements executes if condition1 is false and condition2 is true
else :
// statements executes if condition1 is false and condition2 is false
Here is an example
let number = 50;
if number == 50:
echo "50"
elif number == 100:
echo "100"
elif number == 25:
echo "25"
else:
echo "Other Number."