如何从响应中断的内部结构中断外部循环(循环/切换) [英] How to break outer loops from inner structures that respond break (loops/switch)

查看:128
本文介绍了如何从响应中断的内部结构中断外部循环(循环/切换)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在嵌套结构中中断外部循环,该结构会响应Swift中的break语句?

How to I break an outer loop from within an nested structure that responds to the break statement in Swift?

例如:

while someCondition {
    if someOtherCondition {
        switch (someValue) {
            case 0:     // do something
            case 1:     // exit loop
            case 2...5: // do something else
            default:    break
        }
    } else {
        someCondition = false
    }
}

break只会让我离开switch,在Swift中,必须使用它,因为不允许使用空的情况.我如何从switch内部完全退出循环?

The break will only get me out of the switch, and in Swift, it has to be used as empty cases are not allowed. How can I entirely exit the loop from within the switch?

推荐答案

Swift允许

Swift allows for labeled statements. Using a labeled statement, you can specify which which control structure you want to break from no matter how deeply you nest your loops (although, generally, less nesting is better from a readability standpoint). This also works for continue.

示例:

outerLoop: while someCondition {
    if someOtherCondition {
        switch (someValue) {
            case 0:     // do something
            case 1:     break outerLoop // exit loop
            case 2...5: // do something else
            default:    break
        }
    } else {
        someCondition = false
    }
}

这篇关于如何从响应中断的内部结构中断外部循环(循环/切换)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆