继续使用在switch语句 [英] Using continue in a switch statement

查看:130
本文介绍了继续使用在switch语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从开关语句的中间跳,到循环语句在以下code:

I want to jump from the middle of a switch statement, to the loop statement in the following code:

while (something = get_something())
{
    switch (something)
    {
    case A:
    case B:
        break;
    default:
        // get another something and try again
        continue;
    }
    // do something for a handled something
    do_something();
}

这是使用继续有效的方式?是继续开关语句忽略报表?难道C和C ++在这里他们的行为有什么不同?

Is this a valid way to use continue? Are continue statements ignored by switch statements? Do C and C++ differ on their behaviour here?

推荐答案

这是精细,继续语句涉及封闭的循环,你的code应该相当于(避免此类跳转语句):

It's fine, the continue statement relates to the enclosing loop, and your code should be equivalent to (avoiding such jump statements):

while (something = get_something()) {
    if (something == A || something == B)
        do_something();
}

但是,如果你希望来退出循环,因为您的评论建议(它总是与其他东西再次尝试,直到它计算为false),则需要不同的结构。

But if you expect break to exit the loop, as your comment suggest (it always tries again with another something, until it evaluates to false), you'll need a different structure.

例如:

do {
    something = get_something();
} while (!(something == A || something == B));
do_something();

这篇关于继续使用在switch语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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