Mastering macOS Programming
上QQ阅读APP看书,第一时间看更新

The break statement

We can add a break statement where we want to exit a block of code before it finishes:

switch a 
{
case (let a) where a >= 1:
print("a is a positive integer")
if a >= 10
{
break
}
print("a is less than 10")
default:
print("a is zero or less")
}

A second, perhaps more frequent use of break is to ignore some cases. Since switch statements must be exhaustive, we cannot simply leave them out, but we can insert break into whatever cases we wish to ignore:

switch a 
{
case 0:
print("can't divide by this")
case 1:
print("dividing by one is pointless")
default:
break
}