DisplayScript

Control Flow

Programs normally run statement by statement, from top to bottom. Control flow statements let you change the flow of execution.

Conditionals: if and else

The if statement runs a sequence of statements only when a particular condition holds. The if is followed by a condition expression, then a sequence of statements between braces.

var age = 38
if age >= 21 {
    show("Yep, you're old enough to drink.")
}

The statements in braces only run when the condition is true. Use else to run alternate statements only when the condition is false:

var age = 18
if age >= 21 {
    show("Yep, you're old enough to drink.")
} else {
    show("Sorry, looks like you're underage!")
}

You can chain multiple conditions using else if.

var age = 0.5
if age >= 21 {
    show("Yep, you're old enough to drink.")
} else if age >= 1 {
    show("Sorry, looks like you're underage!")
} else {
    show("What are you doing here. you are a baby.")
}

Each else if condition is evaluated only if all the earlier conditions were false.

If a condition is any value other than true or false, if will raise an error:

var age = 38
if age {
    show("This doesn't work: 38 is neither true nor false.")
}

You can read more about true and false values, or "Booleans", on the Types of Values page.

The switch Statement

The switch statement matches a value against a series of cases.

var windowsVersion = 10
switch windowsVersion {
case 7:
    show("You're running Windows 7.")
case 8:
    show("Windows 8 detected.")
case 10:
    show("Looks like Windows 10.")
}

Once a matching case is reached, the corresponding statements are run. If no case matches, the switch statement runs the default statements instead:

var loadingState = #failed
switch loadingState {
case #loading:
    show("Loading...")
case #ready:
    show("Loaded!")
default:
    show("Something unexpected happened.")
}

You can also use switch statements to unpack the value from a variant (in a limited form of pattern matching).

var loadingState = #ready("Here's the loaded data!")
switch loadingState {
case #loading:
    show("Loading...")
case #ready(data):
    // 'data' now refers to the data in the variant.
    show("Loaded data: \(data)")
}

Here, case #ready(data) matches any #ready value. In the statements following the case, data refers to the value inside the variant: "Here's the loaded data!" See more about variants on the Types of Values page.

The break statement leaves a switch statement early. In this example, only "1" is shown:

switch #example {
case #example:
    show("1")
    break
    show("2")
}

Looping with for

The for statement runs a sequence of statements multiple times: once for each value in an array.

// Shows 1, then 2, then 5.
for number in [1, 2, 5] {
    show(number)
}

for statements specify a name to use for each array element: in this case, the name is number. Following is in, then the array value itself. With this array value ([1, 2, 5]), the statements between braces are run three times: once with number taking the value 1, a second time with number taking the value 2, then a third time with number taking the value 5.

Use at to name the current index in the loop:

for number at i in [1, 2, 5] {
    show("\(i): \(number)")
}

The index always counts up from 0. In this case, i will be 0 while the statements are run the first time, then 1 the second time, then 2 the third time through.

You can use the break statement to exit a for loop early.

for number in [1, 2, 5] {
    show(number)
    if number == 2 {
        break
    }
}

This loop will show 1, then 2, but stop before reaching 5.

By default, for loops add the current index to the persistent identifier to make sure each iteration of the loop has a different identity. See the section on for loops in the State and Identity page for more about this.