在下面的 Swift 代码中,为什么程序在到达 Break 时退出 while 循环?难道它不应该只退出 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 - 在下面的 Swift 代码中,为什么程序在到达 Break 时退出 while 循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32492799/