Chapters

Hide chapters

Swift Cookbook

Live Edition · Multiplatform · Swift · Editor agnostic

Use repeat-while Loops in Swift
Written by Team Kodeco

A repeat-while loop in Swift is a control flow statement that repeatedly executes a block of code as long as a given condition is true.

Here’s an example of how to use a repeat-while loop to print the numbers 1 through 5:

var number = 1
repeat {
  print(number, terminator: " ")
  number += 1
} while number <= 5
// Output: 1 2 3 4 5 

Note that unlike a traditional while loop, a repeat-while loop will always execute the block of code at least once, before checking the condition.

© 2024 Kodeco Inc.