Swift Tutorial
Swift Data Types
Swift Control Flow
Swift Functions
Swift Collections
Swift OOPs
Swift Additional Topics
In Swift, the repeat-while
loop, formerly known as do-while
in some other languages, is a control flow structure that first executes a block of code and then evaluates a condition to decide whether to repeat the block again.
Here's the basic syntax:
repeat { // code to execute } while condition
The loop executes the code inside the repeat
block and then checks the condition specified in the while
clause. If the condition is true
, the loop will execute the code in the repeat
block again. This will continue until the condition becomes false
.
One notable feature of the repeat-while
loop is that the code inside the loop will always execute at least once, even if the condition is false
to start with, because the condition check happens after the code block is executed.
Here's a simple example:
var counter = 0 repeat { print("Counter is \(counter)") counter += 1 } while counter < 3 // Output: // Counter is 0 // Counter is 1 // Counter is 2
In this example, the code block inside the repeat-while
loop will execute until counter
is no longer less than 3.
To decide between using a repeat-while
loop or a regular while
loop, consider whether you want the loop's code block to execute at least once regardless of the initial condition (repeat-while
) or if you want the condition checked before any execution of the loop's code (while
).
How to use repeat-while loop in Swift:
Description: The repeat-while
loop in Swift repeats a block of code while a condition is true.
var count = 0 repeat { print("Count: \(count)") count += 1 } while count < 5
Comparing repeat-while vs while loop in Swift:
Description: The key difference is that repeat-while
guarantees at least one execution, while while
checks the condition before the first execution.
var num = 5 // Using repeat-while repeat { print("Repeat-while: \(num)") num -= 1 } while num > 0 // Using while var num2 = 5 while num2 > 0 { print("While: \(num2)") num2 -= 1 }
Exiting repeat-while loop with break in Swift:
Description: You can use the break
statement to exit a repeat-while
loop prematurely.
var counter = 0 repeat { print(counter) counter += 1 if counter == 3 { break } } while true
Nested repeat-while loops in Swift:
Description: You can nest repeat-while
loops to create more complex iteration patterns.
var i = 1 repeat { var j = 1 repeat { print("(\(i), \(j))") j += 1 } while j <= 3 i += 1 } while i <= 3
Using continue statement in repeat-while loop:
Description: The continue
statement allows you to skip the rest of the code inside the loop for the current iteration.
var num = 0 repeat { num += 1 if num % 2 == 0 { continue } print(num) } while num < 5
Conditional statements with repeat-while in Swift:
Description: You can use conditional statements inside a repeat-while
loop to control its flow.
var number = 0 repeat { if number % 2 == 0 { print("Even") } else { print("Odd") } number += 1 } while number < 3
Swift repeat-while loop vs for loop:
Description: repeat-while
and for
loops are different constructs. repeat-while
checks the condition after each iteration, while a for
loop iterates over a sequence.
// Using repeat-while var i = 0 repeat { print("Repeat-while: \(i)") i += 1 } while i < 3 // Using for loop for j in 0..<3 { print("For loop: \(j)") }