Skip to main content
Version: 2025.0

Control Flow

Drift provides many control flow structures:

  • Loops like for
  • Conditionals like If-Else, Drift-If, Take-If
  • And other statements like break or continue

For

In opposition to many other languages, Drift merges for and for-in statements in one for statement, with a lightweight syntax.

For-loop can be used with any iterable value.

info

Drift will implement Iterable prefab soon. At this moment, Drift has a hard-coded iterator.

With List

By using a List as an iterable value, there are zero to two implementable variables: the value, and the index. Declare more than two loop-variables using a List will throw a runtime exception. If no loop-variable is declared, the value will be stored implicitly into _.

let scores = [10, 50, 23, 100, 59]

// List with one variable
// The variable will store each value
for scores {
print("Score = " + _)
}

// Score = 10
// Score = 50
// Score = 23
// Score = 100
// Score = 59
let scores = [10, 50, 23, 100, 59]

// List with one variable
// The variable will store each value
for scores { as score
print("Score = " + score)
}

// Score = 10
// Score = 50
// Score = 23
// Score = 100
// Score = 59
let scores = [10, 50, 23, 100, 59]

// List with two variables
// The first one will store each value,
// and the second one each loop index
for scores { as score, index
print("Score equals " + score + " at index " + index)
}

// Score equals 10 at index 0
// Score equals 50 at index 1
// Score equals 23 at index 2
// Score equals 100 at index 3
// Score equals 59 at index 4

With Range

Ranges are powerful to represent all integers between two ones. With a Range, for-loop only accepts one parameter: the value. Iterate a Range is straightforward with the Drift's syntax.

for 1..10 {
print("i = " + _)
}

// or

for 1..10 { as i
print("i = " + i)
}

// Will display:
// i = 1
// i = 2
// i = 3
// i = 4
// i = 5
// i = 6
// i = 7
// i = 8
// i = 9
// i = 10

While Loops

While loops allow you to repeat a block of statements as long as the given condition is true. There are two types of while loops:

  • The while loop checks the condition before each iteration and repeats the block while the condition remains true.
  • The repeat-while loop is slightly different: it runs the block once before checking the condition, then continues looping as long as the condition is true.

Conditional

Conditional statements are useful to execute different parts of code based on provided conditions. Drift offers two ways to create a conditional statement:

  • The legacy If-Else statement
  • The Drift conditional expression

Legacy If-Else

info

The Drift syntax convention strongly recommends using the Drift conditional expression instead of this legacy statement, for more human and concise code.

Like most other languages, Drift permits using the If-Else statements:

// Only IF
if condition {
...
}

// IF and ELSE
if condition {
...
} else {
...
}

// IF and ELSE-IF
if condition {
...
} else if condition {
...
}

Drift Conditional Expression (DCE)

The Drift Conditional Expression, shorted as DCE is a ternary-based conditional structure. In other languages, ternary is only used as a value expression. Drift permits using it as a conditional statement also:

// Only THEN branch
condition ? {
...
}

// THEN and ELSE branches
condition ? {
...
} : {
...
}

// THEN and ELSE-THEN branches
condition ? {
...
} : condition ? {
...
}

DCE can also be used as a value expression:

let age = 18
let message = age >= 18 ? "You have +18 y.o." : "You have less than 18 y.o."

// or

let message = age >= 18 ? {
"You have +18 y.o."
} : {
"You have less than 18 y.o."
}

// Braces are optional for DCE branches

By using braces, consider the last branch expression as the value if you use DCE as a value expression. So, it is possible and accepted to do some statements before giving the final value. The version without braces does not support multiple statements in a branch.

let age = 18
var isRestricted: Bool

let message = age >= 18 ? {
isRestricted = false
"You have +18 y.o." // <- It is the value
} : {
isRestricted = true
"You have less than 18 y.o." // <- It is the value
}

Take-If

In some situations, it is interesting to use a value only if the provided condition is true. The Take-If syntax is based on DCE and permits doing it. This behavior is permitted and will return Null if reached.

fun getSecretIfMajor(age: Int) : String? {
return age >= 18 ? "This is a secret!"
// If age >= 18 is true, the string will be returned.
// Else, Null will be returned.
}

Control Transfer Statements (CTS)

CTS permits changing an executed code behavior by transferring control from a part of code to another. Drift has one CTS:

  • return
info

Other CTS will be implemented in the future.