Stop...Hammer Time
It’s important to have some control over your switch
statements and loops. You can use break
, continue
, and labels to control them.
Using break
Using break
stops any kind of loop (for
, for in
, or while
) from carrying on. Say that you’ve found what you were looking for, and you no longer need to waste time or resources looping through whatever items remain. Here’s what you can do:
var mystery = 5for i in 1...8 { if i == mystery { break } println(i) // Will be 1, 2, 3, 4}
The loop will never print 5 and will never loop through 6, 7, or 8.
Using continue
Much like break
, continue
will skip to the next loop and not execute any code below the continue
. If you start with the previous ...
Get Learning Swift™ Programming 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.