Easy Tutorial
❮ Swift Break Statement Swift Arc ❯

Swift Loops

At times, we may need to execute the same block of code multiple times. Ordinarily, statements are executed sequentially: the first statement in a function is executed first, followed by the second, and so on.

Programming languages provide various control structures that allow for more complicated execution paths.

Loop statements enable us to execute a statement or group of statements multiple times. Below is a flowchart of loop statements in most programming languages:

Loop Types

Swift provides the following types of loops. Click on the links to see detailed descriptions of each type:

Loop Type Description
for-in Iterates over all elements in a collection, such as a range of numbers, elements in an array, or characters in a string.
for loop This type of loop has been deprecated in Swift 3. It was used to repeatedly execute a series of statements until a specific condition was met, typically by incrementing a counter after each loop iteration.
while loop Executes a set of statements repeatedly as long as the condition is true, until the condition becomes false.
repeat...while loop Similar to the while statement, but it executes the loop block once before checking the condition.

Loop Control Statements

Loop control statements alter the execution sequence of your code, allowing you to jump to different parts of the code. Swift provides the following loop control statements:

Control Statement Description
continue statement Instructs a loop to stop the current iteration and immediately start the next iteration.
break statement Terminates the current loop.
fallthrough statement If a case is executed and you want to continue executing the next case, you need to use the fallthrough keyword.
❮ Swift Break Statement Swift Arc ❯