Swift while Loop Tutorial 🎯

beginner
22 min

Swift while Loop Tutorial 🎯

Welcome to the Swift while Loop tutorial! In this lesson, we'll dive deep into understanding the while loop in Swift, one of the essential control structures in programming. By the end of this tutorial, you'll be able to use while loops confidently to solve various real-world problems. 💡 Pro Tip: Don't forget to practice coding to solidify your knowledge!

Table of Contents

  1. What is a Loop?
  2. Understanding the while Loop
  3. Syntax of the while Loop
  4. Basic Example
  5. Advanced Example: Factorial Calculation
  6. Breaking out of a Loop
  7. Quiz

What is a Loop? 📝

In programming, a loop is a control structure that allows a section of code to repeat until a certain condition is met. Loops are crucial for automating repetitive tasks, making your code more efficient, and simplifying complex operations.


Understanding the while Loop 💡

The while loop in Swift is used when we want to repeatedly execute a block of code as long as a specific condition remains true. The loop will continue running as long as the condition is met, and it will stop once the condition is no longer true.


Syntax of the while Loop 📝

Here's the basic syntax of a while loop in Swift:

swift
while condition { // Code to be executed }

In the above syntax, condition is a boolean expression that determines whether the loop will continue executing or not.


Basic Example 💡

Let's take an example where we want to print numbers from 1 to 10 using a while loop.

swift
var number = 1 while number <= 10 { print(number) number += 1 }

In the above example, we start with number equal to 1. As long as number is less than or equal to 10, we print the value of number, increment it by 1, and the loop continues. Once number is 11, the loop stops, and the program moves on to the next line of code.


Advanced Example: Factorial Calculation 💡

Now, let's delve into an advanced example where we calculate the factorial of a number using a while loop.

swift
var number = 5 var factorial = 1 while number > 0 { factorial *= number number -= 1 } print("The factorial of 5 is: \(factorial)")

In this example, we start by setting number to 5 and factorial to 1. The while loop continues as long as number is greater than 0. In each iteration, we multiply factorial by number, decrement number by 1, and the loop continues. Once number is 0, the loop stops, and we print the calculated factorial.


Breaking out of a Loop 💡

Sometimes, you might want to stop a loop earlier than its original condition. Swift provides the break statement to allow you to do this. Here's an example:

swift
var number = 1 while true { print(number) number += 1 if number > 10 { break } }

In this example, we have created an infinite loop by setting the condition to true. We print the number and increment it by 1. But, as soon as the number is greater than 10, the if condition is met, and the loop stops using the break statement.


Quiz 🎯

Quick Quiz
Question 1 of 1

Which control structure in Swift allows a section of code to repeat until a certain condition is met?