Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Can case within switch statement specify range of values ?

Posted on 2016-08-11
3
Medium Priority
?
128 Views
Last Modified: 2016-08-11
signed short  position;

switch (position)
{
   case ( 500 to 1250)
   {
         /* set some variable */
         break;
   }

   case (100 to 499)
   {
         /* set some variable */
        break;
   }

   case (-200 to 99)
   {
        /* set some variable */
       break;
   }

   default:
   {
       /* set some variable */
       break;
   }
}

Open in new window


If not, what other constructs can be used besides if - else ?
0
Comment
Question by:naseeam
  • 2
3 Comments
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 41752771
No.  

The C switch statement requires exact values, though you can place multiple values in the same block.

For what you're describing, the structure

  if () else if ()

is probably the best.


Kent
1
 
LVL 1

Author Comment

by:naseeam
ID: 41752798
could you provide example of placing multiple values in same block?
0
 
LVL 46

Accepted Solution

by:
Kent Olsen earned 2000 total points
ID: 41752807
Sure, but it probably doesn't translate well to as many values as your example suggests

switch (Status)
{
  case 0:
  case 1:
  case 12:
    do_something ();
    break;

  case 2:
  case 11:
    do_something_else ();
    break;

  default:
//    do_nothing ();
    break;
}

I don't know that there's a limit to the number of times that case case occur in a switch statement, but the practical limit is probably a lot smaller than the language or architecture limit.


Kent
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses

783 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question