Exploring loops
In programming, you frequently need to do the same thing repeatedly. 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 loop types: 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 if the loop condition is true
.
For more information on loops, visit https://docs.swift.org/swift-book/documentation/the-swift-programming-language/controlflow.
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.
The for-in loop
The for-in
loop steps through every value in a...