0x00 Lesson

You can exit a loop at any time using the ​​break​​​ keyword. To try this out, let’s start with a regular ​​while​​ loop that counts down for a rocket launch:

var countdown = 10
while countdown >= 0 {
print (countdown)
countdown -= 1
}
print("Blast off!")

In this case, the astronaut in command gets bored part-way through the countdown and decides to skip the remainder and launch straight away:

while countdown >= 0 {
print(countdown)
if countdown == 4 {
print("I'm bored. Let's go now!")
break
}
countdown -= 1
}

With that change, as soon as ​​countDown​​ reaches 4 the astronaut’s message will be printed, and the rest of the loop gets skipped


0x01 我的小作品

欢迎体验我的作品之一:​​小编辑器-XCompiler​​​ 在线编辑器~小而巧
​App Store​​ 搜索即可~