在下面的Swift代码中,为什么while循环到了break就退出了?
In the following Swift code why does the program exit while loop when it reaches break?
在下面的Swift代码中,为什么while循环到了break程序就退出了?它不应该只退出 if 而不是 while 循环吗?
var cokeLeft = 7
var fantasLeft = 4
while (cokeLeft > 0) {
println("You have \(cokeLeft) Cokes left.")
cokeLeft = cokeLeft - 1
if(cokeLeft <= fantasLeft){
break
}
}
println("You stop drinking Cokes.")
中断通常退出最近的循环。 while 是一个循环。 if 不被认为是一个循环。这里是对应的文档。
From the IOS Developer Library:
A break statement ends program execution of a loop or a switch
statement.
此外,from the IOS Developer Library:
Loop statements allow a block of code to be executed repeatedly,
depending on the conditions specified in the loop. Swift has four loop
statements: a for statement, a for-in statement, a while statement,
and a do-while statement.
所以,在这里你可以看到循环语句中没有列出 "if"。
在下面的Swift代码中,为什么while循环到了break程序就退出了?它不应该只退出 if 而不是 while 循环吗?
var cokeLeft = 7
var fantasLeft = 4
while (cokeLeft > 0) {
println("You have \(cokeLeft) Cokes left.")
cokeLeft = cokeLeft - 1
if(cokeLeft <= fantasLeft){
break
}
}
println("You stop drinking Cokes.")
中断通常退出最近的循环。 while 是一个循环。 if 不被认为是一个循环。这里是对应的文档。
From the IOS Developer Library:
A break statement ends program execution of a loop or a switch statement.
此外,from the IOS Developer Library:
Loop statements allow a block of code to be executed repeatedly, depending on the conditions specified in the loop. Swift has four loop statements: a for statement, a for-in statement, a while statement, and a do-while statement.
所以,在这里你可以看到循环语句中没有列出 "if"。