Solved

Can case within switch statement specify range of values ?

Posted on 2016-08-11
3
116 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 45

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 45

Accepted Solution

by:
Kent Olsen 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

Technology Partners: 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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
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 and use conditional statements in the C programming language.

623 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