1

So I have some C# code that looks like this:

while(condition)
{
    switch(anumber)
    {
        case 0:
            //do something
            break;
        case 1:
            //do something
            break;
        //and so on
    }
}

Being a noob at programming, I recently added the keyword continue to my vocabulary. After doing some research I came across this:

the continue statement relates to the enclosing loop

So my code should work like this as well:

while(condition)
{
    switch(anumber)
    {
        case 0:
            //do something
            continue;
        //and so on
    }
}

But writing code that just gives no compiler error is not everything. Is using continue in a loop enclosed switch block a good idea? Is there any difference in, for example, performance or are these just two syntactically different but otherwise quite similar ways to achieve the same result?

Wilsu
  • 348
  • 1
  • 3
  • 18
  • 1
    First, it is syntactically correct. Talking about an idea, in general - it is not a sort of bad practice to use `continue` within a `switch` statement. However, it worsens readbility and compilcates debugging and I wouldn't use it. `continue` belongs to `while`, and therefore destroys the structure nesting. Moreover, if you only have `switch` within this loop, then you don't need this `conitnue` at all. `break` will provide the same behaviour, since there are no other statements. – Yeldar Kurmangaliyev Nov 23 '15 at 09:20

2 Answers2

5

If there are some lines of code after the switch, continue keyword will ignore them. Try this and you will see the different:

while(condition)
{
    switch(anumber)
    {
        case 0:
            //do something
            break;
        case 1:
            //do something
            break;
        //and so on
    }
    Console.WriteLine("it's a message");
}

and

while(condition)
{
    switch(anumber)
    {
        case 0:
            //do something
            continue;
        case 1:
            //do something
            continue;
        //and so on
    }
    Console.WriteLine("it's a message");
}
tdat00
  • 810
  • 1
  • 8
  • 11
  • There is a big difference between break and continue in your examples. If you look at carefully at the first example break is a part of switch statement. In the second example continue is a part of while loop. In the first example break is matched with every case and in the second example continue is matched with while loop. – MistyK Nov 23 '15 at 09:20
  • 3
    Yes, I know that. I just want to show the different to OP. – tdat00 Nov 23 '15 at 09:22
  • Also, if continue and break are both used, this might be confusing because they are not referring to the same structure, as they usually would be. – JSQuareD Nov 23 '15 at 09:23
  • You're right, I totally didn't think of that. But I think I'll stick with the break for the sake of readability if that's the only difference in behaviour. – Wilsu Nov 23 '15 at 09:27
  • A switch statement is not a loop, When you code the Continue keyword in a switch statement block it relates to the loop which encloses the switch statement. In short when you use the key word continue it restarts the loop currently being interpreted. – Needham Nov 23 '15 at 09:35
0

https://stackoverflow.com/a/20019538/187650

The continue statement is related to break, but less often used; it causes the next iteration of the enclosing for, while, or do loop to begin. In the while and do, this means that the test part is executed immediately; in the for, control passes to the increment step.

The continue statement applies only to loops, not to a switch statement. A continue inside a switch inside a loop causes the next loop iteration.

This above is for C++ but in case of C# if you debug in Visual Studio you will see also a difference when pressing F10.

When debugging the break statement it will first go to the closing '}' tag of the switch statement before going to the while/for-loop.

In case of continue, the closing '}' tag of the switch statement will not be hit and it will go directly to the while/for-loop to continue the next iteration.

juFo
  • 17,849
  • 10
  • 105
  • 142