do while loop in Swift

IosSwift

Ios Problem Overview


How to write a do-while loop in Swift?

Ios Solutions


Solution 1 - Ios

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.

Solution 2 - Ios

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

Solution 3 - Ios

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]

Solution 4 - Ios

swift's repeat-while loop is similar to a do-while loop in other language . The repeat-while loop is a alternate while loop.It first makes a single pass through the loop block ,then considers the loop condition and repeats the loop till the condition shows as false.

repeat 
{
x--
}while x > 0

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSazzad Hissain KhanView Question on Stackoverflow
Solution 1 - IosSazzad Hissain KhanView Answer on Stackoverflow
Solution 2 - IosDhaval GevariyaView Answer on Stackoverflow
Solution 3 - IosX.CreatesView Answer on Stackoverflow
Solution 4 - IosRakeshDipunaView Answer on Stackoverflow