Types of Loops in Swift

Types of Loops:

When you are programming with loops, it helps to understand that there are only a few basic formats that are used repeatedly. If you can master these variations and recognize when they are needed, then programming will become much easier. All loops have a basic structure. There will be a variable that will control how many times the loop runs. It is necessary to do the following to this variable in each loop that you write.

1. For-in-Loop:

You use the for-in loop over a sequence, such as items in an array, ranges of numbers or characters in a string.


let name = [“Anna”, “Alex”, “Brian”, “Jack”]
for name in names {
print(“Hello, \(name) !”)

2. While Loop:

A while loop performs a set of statements until a condition becomes false. These kinds of loops are best used when the number of iterations is not known before the first iteration begins. A while loop starts by evaluating a single condition. If the condition is true, a set of statements is repeated until the condition becomes false.

3. Repeat-While Loop:

The other variation of the while loop, known as the repeat-while loop. It performs a single pass through the loop block first, before considering the loop’s condition. It then continues to repeat the loop until the condition is false.

Note: The repeat-while loop in Swift is analogous to a do-while loop in other languages. Here’s the general form of a repeat-while loop.

repeat {
} while condition

4. Count Loop:

These are loops that everyone seems to love. People love them so much that they try to use them inappropriately. The only time to use a count loop is when the program can determine ahead of time how many times the loop will repeat. This is not always possible. There are generally two ways that the number of repetition of a loop will be know ahead of time.

1. The loop always repeats the same number of times.

2. The program calculate the number of repetition based upon user input.

5. Sentinel Loop:

Often, it will be necessary to write loops that repeat until a certain value is entered. These are know as sentinel loops. Think of sentinel as a red flag. The loop will continue until you wave a red flag. The sentinel can be a single value or it can be an entire range of values. Sentinel loops are more useful than count loops, since they allow for an undetermined amount of repetitions. There are two common places for using a sentinel loop.

1. Reading an unknown amount of input from the user. This is the most flexible way to read data from the user. The program will be able to accept any amount of input from the user.

2. Validating input: If it is necessary to verify that a user’s input fails within a certain range, then a sentinel loop is required. It is now known ahead of time how many times the user will enter an invalid number. It is not enough to validate the first input from the user. What if the user types another invalid input?

Sentinel loops still have three steps for the loop control variable: initialize, test and update. However, with sentinel loops, the initialize and update statements look the same. This can be a little confusing the first time it is seen, but if you remember that one statement is being used to initialize the loop control variable and the other is being used to update it, then it may make a little more sense.