Control flow statements

Control flow statements are used to control the flow of execution in a program. There are three types of control flow statements in Zing: loop statements, branch statements, and control transfer statements.

Loop statements

For-in loop

The for-in loop allows you to execute a block of statements multiple times, once for each value in a sequence, such as a range of numbers, or the values in an array.

for var index in 1...3 {
    System.log("index is", index)
}
// index is 1
// index is 2
// index is 3

The for-in loop looks like this:

for initializer in expression {
    statements
}

The source expression is only executed once, at the beginning of the loop. Modifications made to the source during the loop will not effect it.

The loop will execute once for each value in the source sequence. If the sequence is empty, the loop will not run at all and execution will continue following the closing brace. For each loop, the sequence value will be assigned to the initializer expression, and then the body statements will be executed.

This example iterates over the names array, and greets each name:

var names = ["Aaron", "Anna", "Sam", "Sarah"]
for var name in names {
    System.log("Hello,", name)
}
// Hello, Aaron
// Hello, Anna
// Hello, Sam
// Hello, Sarah

Zing also supports C-style for loops.

While loop

var result = 1
var factor = 4
while factor > 1 {
    result *= factor
    --factor
    System.log("current:", result)
}
// current: 4
// current: 12
// current: 24
while condition {
    statements
}

Zing also support C-style do-while loops.

Branch statements

If statement

if condition {
    statements
} else if condition {
    statements
} else {
    statements
}

Switch statement

A switch statement is used to compare the result of an expression against a collection of possible values.

var animal = "horse"
switch animal {
    case "human", "emu" {
        System.log("This animal has 2 legs")
    }
    case "dog", "horse", "cow" {
        System.log("This animal has 4 legs")
    }
    case "millipede" {
        System.log("A millipede has up to 750 legs!")
    }
    default {
        System.log("I don't know how many legs this animal has")
    }
}
// This animal has 4 legs

Switch statements consist of an expression, and a collection of case clauses like this:

switch expression {
    case value 1 {
        statements for 1
    }
    case value 2 , value 3 {
        statements for 2 or 3
    }
    default {
        statements for other
    }
}

The switch expression is first evaluated, and the result is compared to to each case clause value in order. If a matching value is found, the body statements for that case are run. After the body has run, the switch statement finishes and execution continues after the final closing brace.

If a matching case clause is not found, the default clause, if present, is executed instead.

Unlike switch statements in languages like C and Java, case blocks in Zing do not implicitly fall through by default and consequently do not require a terminating break statement. If you've never used a programming language that does require an explicit break, just pretend this section doesn't exist.

If you need C-style fallthrough behavior, you can use the fallthrough statement. When the fallthrough statement is run, execution jumps immediately to the start of the next case or default block and runs the case's body statements.

The example below uses fallthrough to create a textual description of a number:

var integerToDescribe = 5
var description = "The number " + integerToDescribe + " is"
switch integerToDescribe {
    case 2, 3, 5, 7, 11, 13, 17, 19 {
        description += " a prime number, and also"
        fallthrough
    }
    default {
        description += " an integer."
    }
}
System.log(description)
// prints "The number 5 is a prime number, and also an integer."

Control transfer statements

Continue

A continue statement can only appear within the body of a for-in, while or do-while loop. The continue instructs the loop to skip the remaining body statements, and start again at the beginning of the next iteration.

The following example uses the continue statement to skip all numbers that are not an exact multiple of three:

var input = [15, 32, 21, 12, 92]
for var number in input {
    if number % 3 {
        continue
    }

    var multiple = number / 3
    System.log(multiple + " * 3 = " + number)
}
// 5 * 3 = 15
// 7 * 3 = 21
// 4 * 3 = 12

Break

A break statement can only appear within the body of a for-in, while or do-while loop or within a case block of a switch statement. The break ends the execution of its container statement immediately. Following a break statement, execution resumes directly after the enclosing switch or loop statement.

This example iterates over an array, and terminates the loop when the first multiple of 7 is found:

var input = [12, 16, 43, 56, 27]
for var number in input {
    if (number % 7) == 0 {
        System.log(number + " is the first multiple of 7")
        break
    }
}
// 56 is the first multiple of 7