How to Use the Switch-Case Structure for Multiple-Choice Decisions in C Programming

Piling up a tower of if and if-else statements in C programming can be effective, but it’s not the best way to walk through a multiple-choice decision. The solution offered in the C language is known as the switch-case structure.

How to make a multiple-choice selection with switch-case structure

The switch-case structure allows you to code decisions in a C program based on a single value. It’s the multiple-choice selection statement.

MULTIPLE CHOICE

#include <stdio.h>
int main()
{
  int code;
  printf("Enter the error code (1-3): ");
  scanf("%d",&code);
  switch(code)
  {
    case 1:
      puts("Drive Fault, not your fault.");
      break;
    case 2:
      puts("Illegal format, call a lawyer.");
      break;
    case 3:
      puts("Bad filename, spank it.");
      break;
    default:
      puts("That's not 1, 2, or 3");
  }
  return(0);
}

Exercise 1: Create a new project using the code from Multiple Choice. Just type it in. Build it. Run it a few times, trying various values to see how it responds.

Examine the source code in your editor, where you can reference the line numbers mentioned in the following paragraphs.

The switch-case structure starts at Line 10 with the switch statement. The item it evaluates is enclosed in parentheses. Unlike an if statement, switch eats only a single value. In Line 10, it’s an integer that the user types (read in Line 8).

The case part of the structure is enclosed in curly brackets, between Lines 11 and 23. A case statement shows a single value, such as 1 in Line 12. The value is followed by a colon.

The value specified by each case statement is compared with the item specified in the switch statement. If the values are equal, the statements belonging to case are executed. If not, they’re skipped and the next case value is compared.

The break keyword stops program flow through the switch-case structure. Program flow resumes after the switch-case structure’s final curly bracket, which is Line 24 in Multiple Choice.

After the final comparison, the switch-case structure uses a default item, shown in Line 21. That item’s statements are executed when none of the case comparisons matches. The default item is required in the switch-case structure.

Exercise 2: Construct a program using source code similar to Listing 8-8, but make the input the letters A, B, and C.

  • The comparison being made in a switch-case structure is between the item specified in switch’s parentheses and the item that follows each case keyword. When the comparison is true, which means that both items are equal to each other, the statements belonging to case are executed.

  • The break keyword is used to break the program flow. It can be used in an if structure as well, but mostly it’s found in looping structures.

  • Specify a break after a case comparison’s statements so that the rest of the structure isn’t executed.

The switch-case structure in C programming

And now — presenting the most complex thing in C. Seriously, you’ll find more rules and structure with switch-case than just about any other construct in C. Here’s the skeleton:

switch(expression)
{
  case value1:
    statement(s);
    break;
  case value2:
    statement(s);
    break;
  case value3:
    statement(s);
    break;
  default:
    statement(s);
}

The switch item introduces the structure, which is enclosed by a pair of curly brackets. The structure must contain at least one case statement and the default statement.

The switch statement contains an expression in parentheses. That expression must evaluate to a single value. It can be a variable, a value returned from a function, or a mathematical operation.

A case statement is followed by an immediate value and then a colon. Following the colon are one or more statements. These statements are executed when the immediate value following case matches the switch statement’s expression. Otherwise, the statements are skipped, and the next case statement is evaluated.

The break keyword is used to flee the switch-case structure. Otherwise, program execution falls through the structure.

The default item ends the switch-case structure. It contains statements that are executed when none of the case statements matches. Or, when nothing is left to do, the default item doesn’t require any statements — but it must be specified.

The case portion of a switch-case structure doesn’t make an evaluation. If you need multiple comparisons, use a multiple if-else type of structure.

How to construct a switch-case structure with no breaks

It’s possible to construct a switch-case structure with no break statements. Such a thing can even be useful under special circumstances.

MEAL PLAN DECISIONS

#include <stdio.h>
int main()
{
  char choice;
  puts("Meal Plans:");
  puts("A - Breakfast, Lunch, and Dinner");
  puts("B - Lunch and Dinner only");
  puts("C - Dinner only");
  printf("Your choice: ");
  scanf("%c",&choice);
  printf("You've opted for ");
  switch(choice)
  {
    case 'A':
      printf("Breakfast, ");
    case 'B':
      printf("Lunch and ");
    case 'C':
      printf("Dinner ");
    default:
      printf("as your meal plan.\n");
  }
  return(0);
}

Exercise 3: Create a new project using the source code from Meal Plan Decisions. Build and run.

Exercise 4: If you understand how case statements can fall through, modify Exercise 2 so that both upper- and lowercase letters are evaluated in the switch-case structure.

  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
Advertisement

Inside Dummies.com

Dummies.com Sweepstakes

Win $500. Easy.