- Mastering macOS Programming
- Stuart Grimshaw
- 111字
- 2021-07-02 22:54:30
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
}