4

WRITING CODE THAT LOOPS

Image

You just learned that you use conditional statements to run different branches of code depending on which conditions are met. Loops are another way you can control the order of your programs. With a loop, you can tell the computer to execute a block of code as many times as you want. This lets you reuse chunks of code again and again so that you don’t have to rewrite them!

In Swift, there are two main types of loops: for-in loops and while loops. The for-in loop tells the computer to run a loop a certain number of times, and it’s useful when you know right off the bat how many times you want the code to repeat. The

Get Coding iPhone Apps for Kids, 1st Edition 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.