Swift Tutorial
Swift Data Types
Swift Control Flow
Swift Functions
Swift Collections
Swift OOPs
Swift Additional Topics
In Swift, the while
loop is a control flow structure that repeatedly executes a block of code as long as a given condition is true
. The loop might not execute at all if the condition is false
at the start.
while condition { // code to execute while the condition is true }
Here's a basic example of a while
loop that prints numbers from 1 to 5:
var count = 1 while count <= 5 { print(count) count += 1 } // Outputs: // 1 // 2 // 3 // 4 // 5
Pre-condition Check: The condition of a while
loop is checked before the execution of loop statements. Hence, it's possible that the loop body might not execute at all if the condition is false
to begin with.
Use Cases: The while
loop is often used when the number of iterations is unknown ahead of time. For instance, it can be used when reading data from a stream until the stream is empty.
repeat-while
LoopSwift also provides another variant of the while
loop called the repeat-while
loop (known in some languages as the do-while
loop). This loop first executes the code inside the loop and then checks the condition. This ensures that the loop body is executed at least once.
repeat { // code to execute } while condition
Here's an example to demonstrate the difference. Consider a scenario where count
starts at 6:
var count = 6 while count <= 5 { print("This won't be printed.") count += 1 } repeat { print("This will be printed once.") count += 1 } while count <= 5
In the example above, the code inside the while
loop won't be executed because the condition is false
at the start. However, the repeat-while
loop will execute its body once before checking the condition.
In summary, use the while
loop when you want to repeatedly execute a block of code based on a condition that's checked before each iteration. If you want to ensure the loop body runs at least once, consider using the repeat-while
loop.
How to use while loop in Swift programming:
Description: The while
loop in Swift repeats a block of code as long as a certain condition is true.
var count = 0 while count < 5 { print("Count: \(count)") count += 1 }
Conditional statements with while loop in Swift:
Description: You can use conditional statements within a while
loop to control its flow.
var number = 0 while number < 5 { if number % 2 == 0 { print("Even") } else { print("Odd") } number += 1 }
Using break and continue in Swift while loop:
Description: The break
statement exits the while
loop prematurely, and continue
skips the rest of the code in the current iteration.
var counter = 0 while true { print(counter) counter += 1 if counter == 3 { break // Exits the loop } }
Infinite while loop and exit conditions in Swift:
Description: An infinite while
loop runs indefinitely until a break condition is met.
var i = 0 while true { print("Infinite Loop") i += 1 if i == 5 { break } }
Swift while loop with boolean conditions:
Description: The while
loop can use boolean conditions to control its execution.
var isRunning = true var counter = 0 while isRunning { print("Counter: \(counter)") counter += 1 if counter == 3 { isRunning = false } }
Nested while loops in Swift programming:
Description: You can nest while
loops to create more complex iteration patterns.
var i = 1 while i <= 3 { var j = 1 while j <= 3 { print("(\(i), \(j))") j += 1 } i += 1 }