In this section, we will explore the switch case statements in ALGOL. Switch case statements are used to execute one block of code among many based on the value of a variable or expression. This is particularly useful when you have multiple conditions to check and want to avoid using multiple if-else statements.

Key Concepts

  1. Switch Case Syntax: Understanding the basic structure of switch case statements in ALGOL.
  2. Case Labels: Defining different cases within the switch statement.
  3. Default Case: Handling cases where none of the specified cases match.
  4. Nested Switch Statements: Using switch statements within other switch statements.

Switch Case Syntax

The basic syntax for a switch case statement in ALGOL is as follows:

switch (expression) begin
    case value1: 
        // Code to execute if expression equals value1
    case value2: 
        // Code to execute if expression equals value2
    ...
    default: 
        // Code to execute if expression does not match any case
end switch;

Example

Let's look at a simple example to understand how switch case statements work in ALGOL.

begin
    integer day;
    day := 3;

    switch (day) begin
        case 1: 
            print("Monday");
        case 2: 
            print("Tuesday");
        case 3: 
            print("Wednesday");
        case 4: 
            print("Thursday");
        case 5: 
            print("Friday");
        case 6: 
            print("Saturday");
        case 7: 
            print("Sunday");
        default: 
            print("Invalid day");
    end switch;
end;

In this example, the variable day is set to 3. The switch statement checks the value of day and executes the corresponding case block. Since day is 3, it prints "Wednesday".

Case Labels

Case labels are used to define the different possible values for the expression in the switch statement. Each case label is followed by a colon and the block of code to execute if the expression matches the case label.

Example

begin
    integer grade;
    grade := 85;

    switch (grade) begin
        case 90: 
            print("A");
        case 80: 
            print("B");
        case 70: 
            print("C");
        case 60: 
            print("D");
        default: 
            print("F");
    end switch;
end;

In this example, the variable grade is set to 85. Since there is no case label for 85, the default case is executed, and it prints "F".

Default Case

The default case is optional but recommended. It handles any values that do not match any of the specified case labels. This ensures that the switch statement has a defined behavior for all possible values of the expression.

Example

begin
    integer month;
    month := 13;

    switch (month) begin
        case 1: 
            print("January");
        case 2: 
            print("February");
        case 3: 
            print("March");
        case 4: 
            print("April");
        case 5: 
            print("May");
        case 6: 
            print("June");
        case 7: 
            print("July");
        case 8: 
            print("August");
        case 9: 
            print("September");
        case 10: 
            print("October");
        case 11: 
            print("November");
        case 12: 
            print("December");
        default: 
            print("Invalid month");
    end switch;
end;

In this example, the variable month is set to 13. Since there is no case label for 13, the default case is executed, and it prints "Invalid month".

Nested Switch Statements

Switch statements can be nested within other switch statements. This allows for more complex decision-making processes.

Example

begin
    integer category, item;
    category := 1;
    item := 2;

    switch (category) begin
        case 1: 
            switch (item) begin
                case 1: 
                    print("Electronics: TV");
                case 2: 
                    print("Electronics: Radio");
                default: 
                    print("Electronics: Unknown item");
            end switch;
        case 2: 
            switch (item) begin
                case 1: 
                    print("Clothing: Shirt");
                case 2: 
                    print("Clothing: Pants");
                default: 
                    print("Clothing: Unknown item");
            end switch;
        default: 
            print("Unknown category");
    end switch;
end;

In this example, the outer switch statement checks the category variable, and the inner switch statement checks the item variable. Since category is 1 and item is 2, it prints "Electronics: Radio".

Practical Exercise

Exercise

Write a program that takes an integer input representing a month (1-12) and prints the corresponding season. Use a switch case statement to determine the season.

  • Months 12, 1, 2: Winter
  • Months 3, 4, 5: Spring
  • Months 6, 7, 8: Summer
  • Months 9, 10, 11: Autumn

Solution

begin
    integer month;
    month := 4;  // Change this value to test different months

    switch (month) begin
        case 12, 1, 2: 
            print("Winter");
        case 3, 4, 5: 
            print("Spring");
        case 6, 7, 8: 
            print("Summer");
        case 9, 10, 11: 
            print("Autumn");
        default: 
            print("Invalid month");
    end switch;
end;

In this solution, the switch statement checks the value of month and prints the corresponding season. If the month is not between 1 and 12, it prints "Invalid month".

Common Mistakes and Tips

  • Forgetting the Default Case: Always include a default case to handle unexpected values.
  • Incorrect Case Labels: Ensure that case labels match the possible values of the expression.
  • Nested Switch Complexity: Avoid overly complex nested switch statements as they can be hard to read and maintain.

Conclusion

Switch case statements in ALGOL provide a clear and efficient way to handle multiple conditions. By understanding the syntax, case labels, default cases, and nested switch statements, you can write more readable and maintainable code. Practice with the provided exercises to reinforce your understanding and prepare for more advanced topics.

© Copyright 2024. All rights reserved