How to Use a Switch Statement in C

The switch statement is a powerful programming structure that exists in most programming languages. It will allow you to write a section of code that can process different instructions based on a number of different possibilities for a variable. A switch statement can quickly take the place of a very complicated, slow series of nested "if" statements. A switch programming structure may also be referred to as a select or case statement (from Visual Basic).

Instructions

    • 1

      Decide clearly on your conditions and the code that you want to execute based on those conditions. Keep in mind that a computer will only do exactly what you instruct it to do, so if you are not very clear on what should happen when, you will have no chance to write code that will work correctly. In this example, we want to determine a shipping rate. If the part code is "A." the shipping cost is $5; if the code is "B," the shipping cost is $10; and if the code is "C," the shipping cost is $20.

    • 2

      Write the switch statement followed by the item to be compared in parentheses. The switch command must be written in all lowercase letters or your program will not compile. Inside the parentheses will be the variable, statement, function or calculation that will be evaluated. Example: switch (code)

    • 3

      Add curly braces to enclose all the internal parts of the switch statement. You can have as many or as few statements as you like inside the switch statement, but as with most parts of C code, you need to enclose the section with curly braces to indicate where the block begins and ends. Example: switch (code) { }

    • 4

      Add case statements for each different option you want to check the item for. Each case statement will be followed by the value to compare to and must be followed by a colon. Example:
      switch (code) {
      case 'A':
      case 'B':
      case 'C':
      }

    • 5

      Add code and instructions for each instance:
      switch (code) {
      case 'A': cost = 5;
      case 'B': cost = 10;
      case 'C': cost = 20;
      }

    • 6

      Add break statements if you want the code to only do instructions for one case. With case statements, the code will continue to execute each line until it reaches the end of the switch or reaches a break statement. Without the break statements, the example in Step 5 will always calculate the cost to be 20. For example:
      switch (code) {
      case 'A': cost = 5; break;
      case 'B': cost = 10; break;
      case 'C': cost = 20; break;
      }

Tips & Warnings

  • You can also add a default: case that will be matched if no other case statements matched your option in parentheses.

  • You can place anything in the parentheses that can be evaluated and compared to the case statements.

Related Searches:

Comments

  • sgtjoebear Jun 12, 2010
    lets say you want a program to return a month for a number entered example 5 = may int month; cin >> month; cout

You May Also Like

Related Ads

Featured