Skip to content

Control Flow

Jacopo De Luca edited this page Feb 16, 2021 · 10 revisions

Argon provides a variety of control flow statements. These include C-style for, for-in and loop loops to perform task multiple times, if and switch statements to execute different branches of code based on certain conditions, and statements like break and continue to transfer the flow of execution to another point in the code.

For-In Loops

You use the for-in loop to iterate over a sequence, such as items in an array or set.

This example uses a for-in loop to iterate over the items in an array:

let NOBLE_GAS = ["Helium", "Neon", "Argon", "Krypton", "Xenon"]

var gas

for gas in NOBLE_GAS {
    println(gas)
}

# Helium
# Neon
# Argon
# Krypton
# Xenon

Loop

A loop loops performs a set of statement until a condition becomes false, or until an explicit break statement not being reached (loop without condition). These kind of loops are best used when number of iteration is not known before the first iteration begins.

Conditional Loop

A loop loop starts by evalutating a single condition, if the condition is true, a set of statements is repeated until the condition becomes false.

loop condition {
    statements
}

var i = 0

loop i < 5 {
    println(i++)
}

Infinite Loop

A loop loop without a condition is a infinite loop, you can exit from infinite loop with break keyword.

var i = 0

loop {
    println(i++)
    if i >= 5 {
        break
    }
}

Conditional Statements

It is often useful to execute different pieces of code based on certain conditions. You might want to run an extra piece of code when an error occurs, or to display a message when a value is reached. To do this, you need to use a condition. Argon provides two ways to add conditional branches to your code, the if statement and the switch statement. Typically, you use the if to evalutate simple condition with few possibile outcomes, instead switch statement is better suited to more complex condition with multiple possible permutations.

If

In its simplets form, the if statement has a single condition. It executes the body statements only if the condition is true.

var pm10 = 51

if pm10 > 50 { println("exceeded the PM10 daily limit") } 

Obviously, if statement can provide an alternative set of statements, known as an else clause, for situations when the if condition is false.

var pm10 = 36

if pm10 > 50 { 
    println("exceeded the PM10 daily limit") 
} else {
    println("PM10 within the allowed limits") 
}

You also can chain multiple if statements together to consider additional clauses.

var pm10 = 36

if pm10 > 50 { 
    println("exceeded the PM10 daily limit") 
} elif pm10 == 50 {
    println("PM10 Pm10 at the allowed limit.") 
} else {
    println("PM10 within the allowed limits") 
}

Remember: the final else clause is always optional!

Switch

A switch statement provides an alternative and shorter way to write a sequence of if - elif statements. It runs the first case whose value is equal to the condition expression.

let os = get_os()

switch os {
   case "darwin":
       println("Mac OS")
   case "linux":
       println("Linux")
   default:
       println(os)
}

Switch cases evaluate cases from top -> bottom, stopping when a case succeeds.

Switch without a condition is a clean way to write long if-elif-else chains.

let os = get_os()

switch {
   case os == "darwin":
       println("Mac OS")
   case os == "linux":
       println("Linux")
   default:
       println(os)
}

In contrast with switch statements in other languages like C/C++, Java..., switch statements in Argon do not fall through the bottom of each case and into the next one by default. Instead, the entire switch statement finishes its execution as soon as the first matching switch case is completed. No explicit break are required.

let os = "linux"

switch os {
   case "linux":
   case "Linux":
       println("Linux")
   default:
       println(os)
}

In the example above, the switch statement does not match both "linux" and "Linux" but simply do notthing. In this way Argon avoids accidental fallthrough from one case to another. To make switch to match both "linux" and "Linux", combine the two values into a compound case, separating the values with semicolon (;).

let os = "linux"

switch os {
   case "linux"; "Linux":
       println("Linux")
   default:
       println(os)
}

Or it is possible to use explicit fallthrough keyword to fall through the case into the next one.

let os = "linux"

switch os {
   case "linux":
       fallthrough
   case "Linux":
       println("Linux")
   default:
       println(os)
}

Remember: fallthrough keyword must be appear only at the end of a switch case, no other statement can follow fallthrough keyword.

Clone this wiki locally