3.2 While Loops

Loop while

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

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