7.4 Breaking from Loops
Having created a loop, it is possible that under certain conditions you might want to break out of the loop before the completion criteria have been met (particularly if you have created an infinite loop). One such example might involve continually checking for activity on a network socket. Once activity has been detected it will most likely be necessary to break out of the monitoring loop and perform some other task.
For the purpose of breaking out of a loop, Swift provides the break statement which breaks out of the current loop and resumes execution at the code directly after the loop. For example:
var j = 10
for _ in 0 ..< 100
{
j += j
if j > 100 {
break
}
print("j = \(j)")
}
In the above example the loop will continue to execute...