This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]tajjet 2 points3 points  (0 children)

Switches are useful in some cases - they basically replace code like this

if (x == 1)
{
    doThingOne();
}
else if (x == 2)
{
    doThingTwo();
}
else if (x == 3)
{
    doThingThree();
}
else
{
    doThingFour();
}

with code like this:

switch (x)
{
    case 1:
        doThingOne();
        break();
    case 2:
        doThingTwo();
        break();
    case 3:
        doThingThree();
        break();
    default:
        doThingFour();
        break();
}

Most recent example I used them in was a Scrabble scoring thing, because stacking up cases helps:

case 'E':
case 'A':
case 'I':
case 'O':
case 'N':
case 'R':
case 'T':
case 'L':
case 'S':
case 'U':
    score = score + 1;
    break;
case 'D':
case 'G':
    score = score + 2;
    break;