CFORBEGINNERS.COM

C Switch Statements

Switch statements are special conditional statements that are sometimes easier to use than an if statements. A switch statement is often used in a menu driven program where a user would make a selection according to their needs. Here is how a switch statement looks.

switch (variable)
{
case 1:
    do stuff;
     break;

case 2:
    do stuff;
     break;

default:
    do stuff;
     break;
}

There can be as many cases as you want, I just happened to put only two for simplicity. After every case there must be a break statement (we will talk more about breaks in the loops lesson) in a nutshell, a break will take you out of the switch and on with the rest of the program. A case is similar to an if statement, case 1 is checking if the variable is 1, if so it will execute everything before the break, case 2 is checking if the variable is 2, and so on. Also notice how there is a colon after the case not a semicolon. Once you are done with your cases you must use a default, this is similar to the else statement, the default is basically saying if not case 1 or case 2 then this. Now lets take a look at a program using a switch statement.


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

int main()
{
int z;
double x,y;
  printf("Enter 1 for MPH to KPH or 2 for KPH to MPH\n");
  scanf("%i",&z);
 
switch (z) /* the variable z will be checked for each case*/
{
  case 1: /* case 1 replaced the if statement that checked to see if z was equal to 1*/
  
  printf("Please enter a speed in miles per hour\n");
  scanf("%lf",&x);
  y=1.609344 * x;
  printf("The equivalent speed in kilometers per hour is %.2f\n",y);
  break;
  

  case 2: /*case 2 replaced the if statement that check to see if z was to 2*/
  
   printf("Please enter a speed in kilometers per hour\n");
   scanf("%lf",&x);
   y = x/1.609344;
   printf("The equivalent speed in miles per hour is %.2f\n",y);
  break;
  
  
 default: /*default replaced the else statement*/
      
         printf("Invalid input\n");
         break;
      }
system ("pause");
return 0;
}

Something important to note, remember how in every conditional statement that had more than one line it was required to put them in curly brackets, here is to refresh your memory.

if (x>y)
{ do stuff; do more stuff; }

A switch statement works a little differently, instead of putting curly brackets for each case you instead put only one set of curly brackets for the whole switch statement. The reason why you do not need curly brackets for each case is because of the break. The break is indicating that the case is done (which is what usually the curly brackets are for).

IMPORTANT!

Unlike condtional statements you cannot use cases exactly the same way, remember how you can use an if statement to check for more than one situation such as.

if (x <= 10);

    /*This is saying to execute the statement for all values less than or equal to x.*/

  case: (x <= 10) /*DOES NOT  WORK*/

Cases have to equal an exact value such as 1,2,3, and so on, cases can't have condtions. This is where conditional statements offer a lot more flexibility in program.