Lab - Switch Statements in C

Objective

The objective of this lab is to introduce students to switch statements in the C programming language. Students will learn how to use switch statements to execute different code blocks based on the value of a variable.

Step 1 - Create a Function

Start by creating a simple function that uses a switch statement to execute different code blocks based on the value of a variable.

#include <stdio.h>

void printColor(int color) {
    switch(color) {
        case 1:
            printf("You selected red.\n");
            break;
        case 2:
            printf("You selected blue.\n");
            break;
        case 3:
            printf("You selected green.\n");
            break;
        default:
            printf("Invalid color selection.\n");
    }
}

In this code, the printColor() function takes an integer parameter color and uses a switch statement to print a message based on the value of color. The main() function calls printColor() with a value of 2 for color.

Step 2 - Add a main program and test

Next add the following main program to your code in the Jdoodle window.

int main() {
    int color = 2;
    printColor(color);
    return 0;
}

Step 3 - Run the Program

Compile and run your program by clicking the Execute button in the Jdoodle window.

You should see the following output:

You selected blue.

You can experiment with other values and test the output.

Step 4 - Validate Input

Now we can add some code to validate the user input.

int main() {
   int color;
   int done;

   done = 0;

   // get prompting for data until the user is done
   while(done!=1)
   {
     printf("Please enter a color (0 to quit): ");
     scanf("%d", &color);

     // check to  see if the user is done
     if(color==0)
     {
       done=1;
     }
     else
     {
       printColor(color);
     }
   }

   return 0;
}

Step 5 - Run The Program

Compile and run the program by clicking the Execute button in the Jdoodle window.

You should see the following output:

Please enter a color (0 to quit): 1
You selected red.
Please enter a color (0 to quit): 2
You selected blue.
Please enter a color (0 to quit): 3
You selected green.
Please enter a color (0 to quit): 0
You can experiment with other values and test the output.

Step 6 - Create a Complex Function

Now let's create a more complex function that uses multiple switch statements to calculate the total cost of an order based on the items ordered and the quantity.

Now enter the following code:

#include <stdio.h>

float calculateOrderCost(int item, int quantity) {
    float cost = 0;
    switch(item) {
        case 1:
            cost += 2.5;
            break;
        case 2:
            cost += 3.0;
            break;
        case 3:
            cost += 4.0;
            break;
        default:
            printf("Invalid item selection.\n");
            return 0;
    }
    switch(quantity) {
        case 1:
            cost *= 1.0;
            break;
        case 2:
            cost *= 0.9;
            break;
        case 3:
            cost *= 0.8;
            break;
        default:
            printf("Invalid quantity selection.\n");
            return 0;
    }
    return cost;
}

In this code, the calculateOrderCost() function takes two integer parameters item and quantity and uses two switch statements to calculate the total cost of an order based on the item and quantity ordered. The main() function calls calculateOrderCost() with a value of 2 for item and 3 for quantity.

Step 7 - Add a Main Function

Next add in a main function to the program and send some test values to the function.

int main() {
    int item = 2;
    int quantity = 3;
    float cost = calculateOrderCost(item, quantity);
    printf("The total cost is: $%.2f\n", cost);
    return 0;
}

Compile and run the code by clicking the Execute button in the Jdoodle window.

The output should be:

The total cost is: $2.40

You can experiment with other values and test the output.

Summary

In this lab, you learned how to use switch statements in the C programming language to execute different code blocks based on the value of a variable. You also learned how to use multiple switch statements to create more complex functions.

File: labc_switch.md
© Copyright 2023 Destin Learning