Solved

Can case within switch statement specify range of values ?

Posted on 2016-08-11
3
70 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 45

Expert Comment

by:Kdo
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
 

Author Comment

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

Accepted Solution

by:
Kdo earned 500 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
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…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

759 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now