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.

Control Transfer Statements

Control transfer statements change the order in which your code is executed, by transferring control from one piece to another. Argon support the following control transfer statements:

  • break
  • continue
  • fallthrough
  • return

Break

break statement ends execution of an entire control flow statement immediately. It can be used inside a switch or loop statement (loop, for, for-in) when you want to terminate the execution of the switch or loop statement erlier.

Continue

continue statement tells a loop to stop what it is doing and start again at the beginning of the next iteration through the loop.

Fallthrough

Enable a switch case to fall through the bottom of each case and into the next one. (See Switch)

Labeled Statements

You can nest loops and conditional statements inside other loops to create a complex control flow structures. However, loops and conditional statements can both use the break statement to end their execution prematurely.

Occasionally it may be useful to specify which loop or conditional statement you want to terminate with break statement, similarly if you have many nested loops, it may be useful to specify which loop the continue statement should affect.

To achieve these aims, you can mark a loop statement or conditional statement with a statement label. And then use the labeled version of the break and continue statements.

outer: loop {
    inner: loop i++ < 100 {
        if i== 50 {
            break outer
        }
    }
}