Using continue in a switch statement

后端 未结 7 521
盖世英雄少女心
盖世英雄少女心 2020-12-13 03:29

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

while (something = get_something())
{
    swi         


        
7条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-13 04:00

    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();
    }
    

    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.

    For example:

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

提交回复
热议问题