// Switch Example C++ Code from CodingMagic.com // Copyright 2008 by Pierre Dufilie IV #include // Include the system() function so we can pause the program #include // Include the printf() function so we can print to the screen int main(void) { // The main function - this is where the program begins running int n; // Declare an instance of an integer variable, n printf("Please enter a number: "); // Use printf to display "Please enter a number: " scanf("%d", &n); // Use scanf to read a number from the console switch (n) { // This switch statement tests the value of n case 1: { // Case where n is equal to 1. printf("n is equal to 1!\n"); // Use printf to display "n is equal to 1!" break; // This break statement is needed to end this case and exit the switch statement. } case 2: { // Case where n is equal to 2. printf("n is equal to 2!\n"); // Use printf to display "n is equal to 2!" break; // This break statement is needed to end this case and exit the switch statement. } case 3: { // Case where n is equal to 3. printf("n is equal to 3!\n"); // Use printf to display "n is equal to 3!" break; // This break statement is needed to end this case and exit the switch statement. } default: { // Default case: if a match for the value of n could not be found in the case statement list.{ printf("n isn't equal to 1, 2, or 3.\n"); // Use printf to display "n isn't equal to 1, 2, or 3." break; // This break statement is needed to end this case and exit the switch statement. } } system("PAUSE"); // Run a system command via system() function to pause execution return 0; // Represents the return value of the program (EXIT_SUCCESS) }