-1

I have this code:

switch (App.co)
{
    case CO.Random:
        var looking = true;
        while (looking)
        {
            // 
            if (xxx)
            {
                looking = false;
            }
        }
        break;

    case CO.FirstToLast:

What I would like to do is to replace it with this:

switch (App.co)
{
    case CO.Random:

        while (true)
        {
            // 
            if (xxx)
            {
                break
            }
        }
        break;

    case CO.FirstToLast:

I am not sure of the way the break is handled and would like to have some confirmation if it's exactly the same to replace the looking flag with a break here

Alan2
  • 23,493
  • 79
  • 256
  • 450

2 Answers2

2

A break is contextual to where it's used. In your first example the case is broken out of. In the second example, the while loop would be broken out of. The same holds true for a for nested in a switch.

This also applies to for, while, switch, etc. blocks - the closest relevant block is exited and execution continues on the next line after that block.

DiskJunky
  • 4,750
  • 3
  • 37
  • 66
1

As per the MSDN:

The break statement terminates the closest enclosing loop or switch statement in which it appears. Control is passed to the statement that follows the terminated statement, if any

Sean
  • 60,939
  • 11
  • 97
  • 136