Blog / January 26, 2023 / 3 mins read / By Mahi Garg

While Loop in Swift

In any programming language, loops are essential for performing repetitive tasks efficiently. Swift offers a variety of loop structures, and one of the most versatile ones is the while loop. In this blog, we will explore the while loop in Swift, its syntax, and various examples to demonstrate how it can be utilized to achieve repetitive operations effectively.

Understanding the while Loop

The while loop is a control flow statement that allows you to execute a block of code repeatedly as long as a specified condition remains true. The loop continues to execute until the condition evaluates to false.

Syntax of the while Loop

The basic syntax of the while loop in Swift is as follows:

while condition {
    // Code to be executed while the condition is true
}

The condition is a Boolean expression that determines whether the loop’s body should be executed. If the condition evaluates to true, the loop’s body is executed, and the process repeats until the condition becomes false.

Examples of while Loop

Example 1 - Printing Numbers:
var number = 1

while number <= 5 {
    print(number)
    number += 1
}

In the above example, the while loop prints the numbers from 1 to 5. The loop starts with number set to 1, and in each iteration, it prints the value of number and increments it by 1. The loop continues executing until number becomes 6, which is when the while condition becomes false, and the loop terminates.

Example 2 - Fibonacci Series:
func fibonacciSeries(_ n: Int) {
    var a = 0, b = 1, c = 0
    
    while c < n {
        print(a)
        c = a + b
        a = b
        b = c
    }
}

fibonacciSeries(20)

In this example, the while loop generates the Fibonacci series up to the value of n. The loop starts with a and b initialized to 0 and 1, respectively. In each iteration, the loop calculates the next Fibonacci number by summing up a and b, updates a and b, and prints the current Fibonacci number. The loop continues until c (the next Fibonacci number) becomes greater than or equal to n.

Infinite Loops and Loop Control Statements

It is essential to be cautious while using while loops to avoid creating infinite loops. An infinite loop is a loop that never terminates because its condition always evaluates to true. For example:

// Infinite Loop - DO NOT USE
while true {
    // Code that keeps executing indefinitely
}

To break out of a loop prematurely, you can use the break statement. To skip the current iteration and continue with the next one, you can use the continue statement.

Conclusion

The while loop in Swift is a powerful construct that allows you to perform repetitive tasks efficiently. By defining a condition that evaluates to true while the loop should continue, you can ensure that the loop executes the necessary code until the condition becomes false.

With the examples provided in this blog, you can now apply the while loop in Swift to various scenarios and achieve efficient repetition in your programs. Always exercise caution with infinite loops and utilize loop control statements (break and continue) when needed to control the flow of your loops effectively. Happy coding! 🚀

Comments