Chapter 8
Repeating Code with Loops
The basic goal of programming is to write as little code as possible that does as much as possible. The less code you write, the easier the program will be to understand and modify later. The more your code does, the more powerful your program will be.
One way to write less code is to reuse code stored in functions. A second way to reuse code is through loops. A loop runs one or more lines of code multiple times, thereby eliminating the need to write multiple, redundant lines of code.
For example, if you wanted to print a message five times, you could write the following:
print ("Hello")print ("Hello")print ("Hello")print ("Hello")print ("Hello")
This is tedious but it will work. However, if you suddenly ...
Get Swift OS X Programming for Absolute Beginners now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.