Computer Languages
Elements - Cases


Case statements are used to define several possible versions of variable that could be input to the computer.
'Switch' is a keyword that tells the compiler that what follows will be a list of these different possibilities.


For example, a program might ask the user to make a decision:

"Press 1 to save, 2 to load or 3 to exit".
The program code below responds accordingly:
    SWITCH userInput

    CASE 1: save
    CASE 2: load
    CASE 3: exit
    DEFAULT: print("Please press 1, 2 or 3")

The syntax of the code will vary depending on the programming language.

The pseudo code above checks the response of the user and carries out the appropriate action. If the user presses any other key then the message "Please press 1,2 or 3" is displayed.