switch

In a number of situations you want to let your instances perform different actions depending on a particular value. You can do this using a number of consecutive if / else statements, but when the possible choices gets above two or three it is usually easier to use the switch statement.

A switch statement has the following syntax:

switch (<expression>)
{
    case <constant1>:
        <code>
    break;

    case <constant2>:
        <code>
    break;
    
    // more cases (with breaks)

    default:
        <code>;
}

This works as follows:

NOTE The switch statement will continue to execute code within a case, until a break is encountered. If you do not use breaks at the end of your cases, it will cause the switch to "leak" to the next case, and even to a default section, if there are no breaks in the way. This can cause unintended behaviour as the execution of one case can result in multiple cases being executed, so ensure to use break where appropriate.

A simple example of using a switch statement would be something like this:

switch (player_lives)
{
    case 3:
        draw_sprite(20, 20, spr_face_healthy);
    break;

    case 2:
        draw_sprite(20, 20, spr_face_hurt);
    break;

    case 1:
        draw_sprite(20, 20, spr_face_fatal);
    break;

    default:
        draw_sprite(20, 20, spr_face_fainted);
    break;
}

Note that multiple case statements can be used to execute the same statement, as the break is not always required for each and every case. If there is no break statement for a particular case, the execution simply continues with the code for the next case, e.g.:

switch (keyboard_key)
{
    case vk_left:
    case ord("A"):
        x -= 4;
    break;

    case vk_right:
    case ord("D"):
        x += 4;
    break;

    case vk_up:
    case ord("W"):
        y -= 4;
    break;

    case vk_down:
    case ord("S"):
        y += 4;
    break;
}

The above code uses switch to check for a keyboard event and then compares that to each case listed. If it meets any of the required values then the corresponding code is executed. Note how the switch can check multiple cases and execute code until the next break, to permit various keys to be used to get the same result.

Each case can have its own code, so you can set up a sort of "inheritance" system where a case executes its own code and then the code for the next case as well.