Posts

Showing posts from October, 2015

Calling another case from one case in Switch statement using C#

Sometimes it might be possible that we need to call some another case of switch statement after some processing in one case. In that case we can use either falling through switch cases or goto case statement.

Here is the example of falling through -

Falling through switch-cases can be achieved by having no code in  case (see case 0 and 1):
switch (/*...*/) {
    case 0: // shares the exact same code as case 2
    case 1:// shares the exact same code as case 2
    case 2:
        // do something else
        break;
    default:
        // do something entirely different
        break;}

Both case 0 and 1 will execute code of case 2.


Using special goto case in switch statement -
 switch (/*...*/) {
    case 0:
        //do something
        break;

    case 1:
       //do something
       break;

    case 2:
        if(condition1)
            goto case 1;
        else
            goto case 2;

        break;
    default:
        // do something entirely different
        break;}

Here in the …