Solved

Can case within switch statement specify range of values ?

Posted on 2016-08-11
3
83 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
 
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:
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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C#, VS15, StructLayout 1 134
Intel debugger, idb, only seeing .c and .h, but no .f90 1 330
Problem to ASCII 1 184
Problem to save 10 167
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…
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 and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

832 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