Sierra Alpha
Sierra Alpha

Reputation: 3727

Switch-Case Statement and Range of Numbers

Is there a way to use switch statement with ranges in Objective C (in XCode), hypothetically something like this:

- (NSString *)evaluate:(NSInteger)sampleSize
{
       NSString returnStr;
       switch (sampleSize)
           {
               case sampleSize < 10: 
               returnStr = @"too small!";
               break;

               case sampleSize >11 && sampleSize <50:
               returnStr = @"appropriate";
               break;

               case sampleSize >50:
               returnStr = @"too big!";
               break;
           }
       return returnStr;
}

Upvotes: 14

Views: 19207

Answers (4)

Adit Bhargava
Adit Bhargava

Reputation: 43

- (NSString *)evaluate:(NSInteger)sampleSize
{
       NSString returnStr;
       switch (sampleSize)
           {

    //    for sampleSize between 0 and 10

               case 0 ... 10: 
               returnStr = @"too small!";
               break;

    //    for sampleSize between 11 and 50

               case 11 ... 50:
               returnStr = @"appropriate";
               break;

    //    for sampleSize above 50

               case 50 :
               case default:
               returnStr = @"too big!";
               break;
           }
       return returnStr;
}

Please Note: This is a solution i worked out but it will not count if sampleSize has h value less than 0.

Upvotes: 4

Jeetendra Chauhan
Jeetendra Chauhan

Reputation: 1977

Simply pass true in your switch statement like below code

- (NSString *)evaluate:(NSInteger)sampleSize {
   NSString returnStr;
   switch (true)
       {
           case sampleSize < 10: 
           returnStr = @"too small!";
           break;

           case sampleSize >11 && sampleSize <50:
           returnStr = @"appropriate";
           break;

           case sampleSize >50:
           returnStr = @"too big!";
           break;
       }
   return returnStr;

}

This will solve your problem

Upvotes: -7

Hugh
Hugh

Reputation: 8932

There is a GCC extension (which I assume is supported in Clang) that might be suitable for you. It allows you to use ranges in case statements. The full documentation is at http://gcc.gnu.org/onlinedocs/gcc-4.2.4/gcc/Case-Ranges.html#Case-Ranges - an example case statement from that page is

case 1 ... 5:

which would match (unsurprisingly) 1, 2, 3, 4, or 5.

Upvotes: 43

SuperTron
SuperTron

Reputation: 4243

No, switch statements are for constant values in most languages... The closest you can get is to flow the cases into one another like this:

switch(sampleSize)
{
    case 0:
    case 1:
    case 2:
        returnStr = @"too small!";
        break;
}

Alternatively, this Question may help...

EDIT: I just thought of another way: you could "#define" that large list of cases in a .h file like this:

#define TOO_LOW     case 0: \
                    case 1: \
                    case 2: \
                    case 3:

and then use it in a switch like so:

switch(sampleSize)
{
    TOO_LOW
        returnStr = @"too small!";
        break;
}

Of course, thats not the cleanest solution. What's wrong with 3 "if/else's"?

Upvotes: 4

Related Questions