Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C - Switch with multiple case numbers

So my professor asked us to create a switch statement. We are allowed to use only the "SWITCH" statement to do the program. He wants us to input a number and then display it if it is on the number range and what briefcase number will be taken as shown below. Now... I know that for this type of program it is easier to use the IF statement. Doing Case 1: Case 2: Case 3...Case 30 will work but will take too much time due to the number range.

#include <stdio.h>
main()
{
      int x;
      char ch1;
      printf("Enter a number: ");
      scanf("%d",&x);
      switch(x)
      {
                 case 1://for the first case #1-30
                 case 30:
                      printf("The number you entered is >= 1 and <= 30");
                      printf("\nTake Briefcase Number 1");
                      break;         
                 case 31://for the second case #31-59
                 case 59:
                      printf("The number you entered is >= 31 and <= 59");
                      printf("\nTake Briefcase Number 2");
                      break;                 
                 case 60://for the third case #60-89
                 case 89:
                      printf("The number you entered is >= 60 and <= 89");
                      printf("\nTake Briefcase Number 3");
                      break;                 
                 case 90://for the fourth case #90-100
                 case 100:
                      printf("The number you entered is >= 90 and <= 100");
                      printf("\nTake Briefcase Number 4");
                      break;      
                 default:
                     printf("Not in the number range");
                     break;

                 }
      getch();
      }

My professor told us that there is a shorter way on how to do this but won't tell us how. The only way I can think of shortening it is by using IF but we are not allowed to. Any Ideas on how I can make this work out?

like image 927
user2649696 Avatar asked Nov 26 '13 04:11

user2649696


People also ask

Can switch case have multiple conditions in C?

A switch statement—or simply a case statement—is a control flow mechanism that determines the execution of a program based on the value of a variable or an expression. Using a switch statement allows you to test multiple conditions and only execute a specific block if the condition is true.

Can switch case have multiple values?

You can use N number of case values for a switch expression. The Case unit must be unique, otherwise, a compile-time error occurred.

How many cases can a switch have C?

Microsoft C doesn't limit the number of case values in a switch statement. The number is limited only by the available memory. ANSI C requires at least 257 case labels be allowed in a switch statement.


3 Answers

With GCC and Clang, you can use case ranges, like this:

switch (x){

case 1 ... 30:
    printf ("The number you entered is >= 1 and <= 30\n");
    break;
}

The only cross-compiler solution is to use case statements like this:

switch (x){

case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
    printf ("The number you entered is >= 1 and <= 6\n");
    break;
}

Edit: Using something to the effect of switch (x / 10) is another good way of doing this. It may be simpler to use GCC case ranges when the ranges aren't differences of 10, but on the other hand your professor might not take a GCC extension as an answer.

like image 178
tay10r Avatar answered Oct 26 '22 23:10

tay10r


If the ranges are consistent, then you can throw away some of the data:

switch (x / 10 )
{
   case 0:
   case 1:
   case 2:  // x is 0 - 29
     break ;

   // etc ...
}

Otherwise you'll have to do a little bit of hackery around the edges.

like image 38
woolstar Avatar answered Oct 27 '22 01:10

woolstar


   Try this ...

#include <stdio.h>
main()
{
      int x;
      char ch1;
      printf("Enter a number: ");
      scanf("%d",&x);
      int y=ceil(x/30.0);
      switch(y)
      {

                 case 1:
                      printf("The number you entered is >= 1 and <= 30");
                      printf("\nTake Briefcase Number 1");
                      break;         

                 case 2:
                      printf("The number you entered is >= 31 and <= 60");
                      printf("\nTake Briefcase Number 2");
                      break;                 

                 case 3:
                      printf("The number you entered is >= 61 and <= 90");
                      printf("\nTake Briefcase Number 3");
                      break;                 

                 case 4:
                      printf("The number you entered is >= 91 and <= 100");
                      printf("\nTake Briefcase Number 4");
                      break;      
                 default:
                     printf("Not in the number range");
                     break;

                 }
      getch();
      }
like image 39
uhs Avatar answered Oct 27 '22 01:10

uhs