Exploring Loops
In programming, you frequently need to do the same thing over and over again. For example, each month, a company will need to generate payroll slips for each employee. If the company has 10,000 employees, it would be inefficient to write 10,000 instructions to create the payroll slips. Repeating a single instruction 10,000 times would be better, and loops are used for this.
There are three types of loop; the for-in
loop, the while
loop, and the repeat-while
loop. The for-in
loop will repeat for a known number of times, and the while
and repeat-while
loops will repeat as long as the loop condition is true.
Important Information
For more information on loops, visit: https://docs.swift.org/swift-book/LanguageGuide/ControlFlow.html.
Let's look at each type in turn, starting with the for-in
loop, which is used when you know how many times a loop should be repeated.
Using the for-in loop
The for-in
loop steps through every value in a sequence, and a...