Control Flow
Repeat While
repeat while runs the loop body once before checking the condition.
Run once before testing
repeat_while.swift
let start =
var countdown = start
var ticks = 0
repeat {
ticks = ticks + 1
countdown = countdown - 1
} while countdown > 0
print("start=\(start)")
print("ticks=\(ticks)")
let start =
var countdown = start
var ticks = 0
repeat {
ticks = ticks + 1
countdown = countdown - 1
} while countdown > 0
print("start=\(start)")
print("ticks=\(ticks)")
let start =
var countdown = start
var ticks = 0
repeat {
ticks = ticks + 1
countdown = countdown - 1
} while countdown > 0
print("start=\(start)")
print("ticks=\(ticks)")
repeat while
A `repeat while` loop is useful when the body must execute at least once, even if the condition becomes false immediately.