Loops In Swift Programming Language

Introduction

If you have any programming experience, then you must have used loops. It is one of the integral parts of any programming language. As we all know, loops are the iterative process of doing something periodically.

In every programming language, we have four types of loops:

  1. repeat-while
  2. While
  3. For
  4. For Each (for-in)

Repeat-While
It’s a Swift version of do-while which is only exit-control loop.


While

It’s an entry controlled loop where a condition is matched first.
 


For Loop

It’s one of the most used loops among all three. You can use any of them as per your comfort level. Most of the programmers use for and while interchangeably.

There is nothing new in for loop syntax apart from the braces. Swift doesn’t use increment/decrement operator(++/--) as Swift doesn’t belong to C Language.

Swift recommends to use for-each (for in) loop instead of for loop. For-each loop is most useful when you are iterating collection type (Array or Dictionary). At present, we are just dealing with the syntax of loops.

However, you can use it with range operator.

Next, we have an array of strings where we’ll iterate through all.