Tuesday, November 8, 2011

Control Structures: Switch

Another type of conditional statement is the switch statement. This is a variation of if/else statements, which is visually more compact and also allows for faster compilation.


Switch Statements
General Structure

    switch (expression)
    {
        case first-constant:
            code
            break

        case second-constant:
            code
            break

        default:
            code
            break
    }


Python

Python does not formally support switch statements. There are ways to emulate them, but I'll leave that for another discussion.


C++

    switch (month)
    {
        case 1:
            cout << "The month is January." << endl;
            break;

        case 2:
            cout << "The month is February." << endl;
            break;

        case 3:
            cout << "The month is March." << endl;
            break;

        case 4:
            cout << "The month is April." << endl;
            break;

        case 5:
            cout << "The month is May." << endl;
            break;

        case 6:
            cout << "The month is June." << endl;
            break;

        case 7:
            cout << "The month is July." << endl;
            break;

        case 8:
            cout << "The month is August." << endl;
            break;

        case 9:
            cout << "The month is September." << endl;
            break;

        case 10:
            cout << "The month is October." << endl;
            break;

        case 11:
            cout << "The month is November." << endl;
            break;

        case 12:
            cout << "The month is December." << endl;
            break;

        default:
            cout << "Invalid month." << endl;
    }


Java

    switch (month) {
        case 1:
            System.out.println("The month is January.");
            break;

        case 2:
            System.out.println("The month is February.");
            break;

        case 3:
            System.out.println("The month is March.");
            break;

        case 4:
            System.out.println("The month is April.");
            break;

        case 5:
            System.out.println("The month is May.");
            break;

        case 6:
            System.out.println("The month is June.");
            break;

        case 7:
            System.out.println("The month is July.");
            break;

        case 8:
            System.out.println("The month is August.");
            break;

        case 9:
            System.out.println("The month is September.");
            break;

        case 10:
            System.out.println("The month is October.");
            break;

        case 11:
            System.out.println("The month is November.");
            break;

        case 12:
            System.out.println("The month is December.");
            break;

        default:
            System.out.println("Invalid month");
            break;
    }

  • Once again, C++ and Java's syntax are remarkably similar. In fact, they are identical other than their print statements. Some things to note:
    • The cases are equivalent to if or else if, the default is equivalent to else.
    • The case expression supports more than just integers (it also supports integer variations and characters), but whatever is used must be a constant.
    • There is NO punctuation after the expression.
    • There is a colon after each case.
    • You also don't need to include the "break" statement under the "default" case since it's the end of the loop.
    • None of the whitespace is necessary. You could just as easily write each case like:
      case 1:    System.out.println("The month is January.");    break;
    • However, I chose to write the examples this way for readability.

No comments:

Post a Comment