The switch statement

Consider the following program segment to do weight conversion, depending on which planet you are on:
/* Illustrating the switch statement

    Assume a program that prompts for your weight on earth and then gives you a
    menu of planet numbers to choose from for converting your weight to what it 
    would be on that planet. One approach is to use multiple if statements:

    // Calculate weight on desired planet given earthweight and chosen planet
    if (menuOption==1) {
       planetWeight = earthWeight * 0.39;    // Mercury
    }
    else if (menuOption==2) {
         planetWeight = earthWeight * 0.91;    // Venus
    }
    else if (menuOption==3) {
         planetWeight = earthWeight * 0.38;    // Mars
    }
    else {
         planetWeight = earthWeight - 1;       // marketing ploy
    }

    Instead, we can use the "switch" statement shown below:
 */

    switch ( menuOption) {
        case 1: planetWeight = earthWeight * 0.39;    // Mercury
             break;
        case 2: planetWeight = earthWeight * 0.91;    // Venus
             break;		
        case 3: planetWeight = earthWeight * 0.38;    // Mars
             break;
        default: planetWeight = earthWeight - 1;        // Earthweight - 1
            // People like to think they're losing weight (marketing)
            break;
    }
    cout << " Your new weight is " << planetWeight "\n";


[CS Dept] [UIC] [Prof. Reed]