Skip to content

Control Flow

Pranav Verma edited this page Nov 26, 2024 · 3 revisions

Tidal includes control flow mechanisms that allow you to make decisions in your code. These include conditional statements like:

  • if
  • elif
  • else

These statements allow your program to execute code blocks based on certain conditions, offering flexibility and power in your logic flow.

if

The if keyword is used to check a condition. If the condition evaluates to true, the block of code following the if statement will be executed.

Syntax

if condition {
    /* code to execute if condition is true */
}

Example:

var x = 5
if x > 3 {
    print("x is greater than 3")
}

In this example, since x is greater than 3, the program will output: x is greater than 3.

elif

The elif keyword (short for "else if") is used to check additional conditions if the previous if or elif condition was false. If the elif condition evaluates to true, its associated block of code will be executed.

Syntax

if condition {
    /* code if condition is true */
} elif another_condition {
    /* code if another_condition is true */
}

Example:

var y = 10
if y < 5 {
    print("y is less than 5")
} elif y < 15 {
    print("y is less than 15")
}

Here, y is not less than 5, so the program checks the next condition, y < 15, which is true. The output will be: y is less than 15.

else

The else keyword is used as a fallback when none of the preceding if or elif conditions are true. The block of code following the else statement will be executed if all conditions were false.

Syntax

if condition {
    /* code if condition is true */
} else {
    /* code if all conditions were false */
}

Example:

var z = 2
if z > 10 {
    print("z is greater than 10")
} else {
    print("z is 10 or less")
}

In this case, z is not greater than 10, so the program will output: z is 10 or less.

Clone this wiki locally