Sometimes, we may need to execute the same piece of code multiple times. Ordinarily, statements are executed sequentially: the first statement in a function is executed first, followed by the second statement, and so on.
Programming languages provide multiple control structures for more complex execution paths.
Loop statements allow us to execute a statement or group of statements multiple times. Here is a flowchart of a loop statement in most programming languages:
Circulation type
The Swift language provides the following types of loops. Click on the links for a detailed description of each type:
Circulation type | describe |
---|---|
for-in | Iterate through all the elements in a collection, such as numeric intervals, elements in arrays, characters in strings. For index in var {body of loop} |
The for loop | This loop has been deprecated in Swift 3. |
The while loop | Run a series of statements, and if the condition is true, repeat until the condition becomes false. Syntax: while condition {statement(s)} |
Others are not commonly used |
Loop control statement
Loop control statements change the order in which your code is executed, so you can jump through it. The following cycle control statements for SWIFT:
Control statements | describe |
---|---|
The continue statement | Tells a loop body to immediately stop the current iteration and restart the next iteration. |
Break statement | Interrupt the current loop. |
Fallthrough statement | If you continue to execute the following cases after a case has finished executing, you need to use the fallthrough keyword. |