Loops are fundamental constructs in programming that allow you to execute a block of code multiple times. Swift provides several types of loops to handle different scenarios. In this section, we will cover the following types of loops:

  1. for-in loops
  2. while loops
  3. repeat-while loops

  1. for-in Loops

The for-in loop is used to iterate over a sequence, such as items in an array, ranges of numbers, or characters in a string.

Syntax

for item in sequence {
    // Code to execute
}

Example

let fruits = ["Apple", "Banana", "Cherry"]

for fruit in fruits {
    print(fruit)
}

Explanation

  • fruits is an array containing three strings.
  • The for-in loop iterates over each element in the fruits array.
  • The print(fruit) statement is executed for each element, printing each fruit to the console.

Practical Exercise

Exercise: Write a for-in loop to print the numbers from 1 to 5.

Solution:

for number in 1...5 {
    print(number)
}

  1. while Loops

The while loop executes a block of code as long as a specified condition is true.

Syntax

while condition {
    // Code to execute
}

Example

var counter = 1

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

Explanation

  • counter is initialized to 1.
  • The while loop checks if counter is less than or equal to 5.
  • The print(counter) statement is executed, and counter is incremented by 1 in each iteration.
  • The loop stops when counter exceeds 5.

Practical Exercise

Exercise: Write a while loop to print the even numbers from 2 to 10.

Solution:

var number = 2

while number <= 10 {
    print(number)
    number += 2
}

  1. repeat-while Loops

The repeat-while loop is similar to the while loop, but it guarantees that the code block is executed at least once before the condition is evaluated.

Syntax

repeat {
    // Code to execute
} while condition

Example

var counter = 1

repeat {
    print(counter)
    counter += 1
} while counter <= 5

Explanation

  • counter is initialized to 1.
  • The repeat block is executed, printing counter and incrementing it by 1.
  • The condition counter <= 5 is checked after the block is executed.
  • The loop stops when counter exceeds 5.

Practical Exercise

Exercise: Write a repeat-while loop to print the numbers from 5 to 1.

Solution:

var number = 5

repeat {
    print(number)
    number -= 1
} while number >= 1

Common Mistakes and Tips

  • Off-by-one errors: Ensure your loop conditions are correctly set to avoid iterating one time too many or too few.
  • Infinite loops: Make sure the loop's condition will eventually become false to avoid infinite loops.
  • Using the correct loop type: Choose the loop type that best fits your scenario. Use for-in for known sequences, while for conditions that need to be checked before each iteration, and repeat-while for conditions that need to be checked after each iteration.

Conclusion

In this section, we covered the three main types of loops in Swift: for-in, while, and repeat-while. We discussed their syntax, provided examples, and included practical exercises to reinforce the concepts. Understanding loops is crucial for controlling the flow of your programs and performing repetitive tasks efficiently. In the next section, we will explore switch statements, another powerful control flow tool in Swift.

© Copyright 2024. All rights reserved