© Wallace Wang 2017

Wallace Wang, macOS Programming for Absolute Beginners, 10.1007/978-1-4842-2662-9_8

8. Repeating Code with Loops

Wallace Wang

(1)San Diego, California, USA

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 to 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 want to print a message five times, you can write the following:

print ...

Get macOS Programming for Absolute Beginners: Developing Apps Using Swift and Xcode now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.