Tracks
/
Swift
Swift
/
Syllabus
/
While and repeat loops
Wh

While and repeat loops in Swift

1 exercise

About While and repeat loops

A while loop is a control flow statement that allows code to be executed while a certain condition is true. Swift provides two kinds of while loops: while and repeat-while loops. While loop's condition is evaluated before the loop body is executed; if the condition is false, the loop body will not be executed at all. While loops in Swift have the following structure:

while <#condition#> {
  <#code#>
}

Here the loop will exectute as long as count is greater than 0, and will thereby print the numbers 2, 1 and "Liftoff!".

var count = 2
while count > 0 {
  print("\(count)…")
  count -= 1
}
print("Liftoff!")

// prints:
// 2…
// 1…
// Liftoff!

A repeat-while loop is similar to a while loop, but the condition is evaluated after the loop body is executed. This means that the loop body will always execute at least once, even if the condition is false. repeat-while loops in Swift have the following structure:

repeat {
  <#code#>
} while <#condition#>

Here the loop will only execute once, since the condition is false. Since it is a repeat-while loop, the loop body will execute once before the condition is evaluated.

var count = 2
repeat {
  print("\(count)…")
} while count < 0
print("Liftoff!")

Optional binding loops

One common variant of while loops in Swift is the optional binding while loop. These loops repeat as long as the call to some function that yields an optional value returns a non-nil value. That (non-optional) value is then boud to a name of your choice and can be used in the body of the loop.

var arr = [1,2]
while let count = arr.popLast() {
  print("\(count)…")
}
print("Liftoff!")

// prints:
// 2…
// 1…
// Liftoff!
Edit via GitHub The link opens in a new window or tab

Learn While and repeat loops