do while loop in Swift

Here’s the general form of a repeat-while loop for Swift

repeat {
    statements
} while condition

For example,

repeat {
    //take input from standard IO into variable n
} while n >= 0;

This loop will repeat for all positive values of n.


repeat-while loop, performs a single pass through the loop block first before considering the loop's condition (exactly what do-while loop does).

var i = Int()
repeat {
//below line was fixed to say print("\(i) * \(i) = \(i * i)")
   print("\(i) * \(i) = \(i * i)")
    i += 1

} while i <= 10

repeat-while loop, performs a single pass through the loop block first before considering the loop's condition (exactly what do-while loop does).

Code snippet below is a general form of a repeat-while loop,

repeat {
  // your logic
} while [condition]

Tags:

Ios

Swift