3.2 While Loops

Loop while

  • execute the code as long as the condition is true
// -> 1 2 3 (i = 4 at end)
var i = 1
while i <= 3 {
  print("i = \(i)")
  i += 1
}

Loop repeat while

  • executes the code before evaluating the condition (formerly do-while)
// -> 1 2 3 (j = 4 at end)
var j = 1
repeat {
  print("j = \(j)")
  j += 1
} while j <= 3