C++ Programming Tutorial Lesson 04: Switch Statements

The switch statement is used in C++ for testing if a variable is equal to one of a set of values. The variable must be an integer, i.e. integral or non-fractional. The programmer can specify the actions taken for each case of the possible values the variable can have. The same operation can be performed by using a series of if, else if, and else statements as was demonstrated in Lesson 03: Else If Statements. Therefore, let's look at an example using the switch statement that mimics the functionality of the Else If Example from Lesson 03.

Switch Example

#include <stdlib.h>
#include <stdio.h>

int main(void) {
   int n;
   printf("Please enter a number: ");
   scanf("%d", &n);
   switch (n) {
      case 1: {
         printf("n is equal to 1!\n");
         break;
      }
      case 2: {
         printf("n is equal to 2!\n");
         break;
      }
      case 3: {
         printf("n is equal to 3!\n");
         break;
      }
      default: {
         printf("n isn't equal to 1, 2, or 3.\n");
         break;
      }
   }
   system("PAUSE");
   return 0;
}
Feel free to download the Switch Example source code directly. This example builds off of the Else If Example source code from Lesson 03: Else If Statements. The program's end functionality has not changed, only the method of implementation in the C++ source code. The program reads user input from the keyboard until the return key is pressed. After the user inputs a number, that value is stored into the integer variable "n". The variable "n" is then tested by a switch statement. If the user inputs the number 1, then "n is equal to 1!" is printed as shown below.



If instead the user inputs the number 2, then "n is equal to 2!" is printed as shown below.



If instead the user inputs the number 3, then "n is equal to 3!" is printed as shown below.



However, if the user inputs any number other than 1, 2, or 3, such as 5, then "n isn't equal to 1, 2, or 3." is printed as shown below.



The table below describes the switch statement C++ code presented in the above example.

Source Code Description & Explanation
switch (n) {
This is the switch statement. It indicates that a series of cases will follow in which we will test for equality with variable "n". If n is equal to any of the values in the following cases, then the first match found will run its encapsulated code. The curly brace, {, encapsulates the case statements. An end curly brace, }, will follow below.
case 1: {
   printf("n is equal to 1!\n");
   break;
}
This is the first case statement. It has a set of code encapsulated by curly braces, { and }, which are not manditory, but serve to make the source code easier to follow. If n is equal to 1, then the printf() function here will display "n is equal to 1!" on the screen. A break statement follows the printf() function. This is required after each case to tell the program to exit out of the switch statement. Otherwise, the rest of the code in each of the cases would be run if the break statement were not present.
case 2: {
   printf("n is equal to 2!\n");
   break;
}
This is the second case statement. If n is equal to 2, then the printf() function will be run to display "n is equal to 2!" on the screen. The break statement will cause the program to exit out of the switch statement as desired since we have already found a match for n.
case 3: {
   printf("n is equal to 3!\n");
   break;
}
This is the last case statement. If n is equal to 3, then the printf() function will be run to display "n is equal to 3!" on the screen. The break statement will cause the program to exit out of the switch statement as desired since we have already found a match for n.
default: {
   printf("n isn't equal to 1, 2, or 3.\n");
   break;
}
This is the default statement. If a match for n has not been found, then the code after the default statement will be run, and the printf() function will display "n isn't equal to 1, 2, or 3." on the screen. The break statement will cause the program to exit out of the switch statement; although, since the default statement here comes after the case statements, this break statement inside of the default statement is technically not required.
}
This is the end curly brace that encapsulates the case statements and the default statement within the switch statement.

Copyright © 2008 Pierre Dufilie IV. All Rights Reserved.