switch case Q

This is a discussion on switch case Q within the C++ Programming forums, part of the General Programming Boards category; For the life of me, I can't figure out an easy way to work a range of values into a ...

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    4

    switch case Q

    For the life of me, I can't figure out an easy way to work a range of values into a single case.


    in visual basic it works something like this:
    Code:
    select case variable
    case 1 to 10
    'do a thing
    case 11 to 20
    'do something else
    case 21 to 30
    'do a third thing
    end select

    I've tried a few ways of doing this, but, as my compiler accepts none, I have to assume that they are wrong.

    Can somebody help please?

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Use an if-else tree.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    4
    I was considering it, but I didnt want to do anything if it were possible to use switch.

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    This is the closest thing in switch-case:
    Code:
    switch(Var)
    {
      case '0':
      case '1':
      case '2':
      {
        break;
      }
    
      case '3':
      case '4':
      case '5':
      {
        break;
      }
    }
    Not well suited for large intervals of course!
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    4
    Thank you, but seeing as the split is from 1 to 100 in varying intervals, I think I'll use ifelse.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How can I make this code more elegant?
    By ejohns85 in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2009, 08:55 AM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 04:42 PM
  3. Replies: 27
    Last Post: 10-11-2006, 04:27 AM
  4. Problems with switch()
    By duvernais28 in forum C Programming
    Replies: 13
    Last Post: 01-28-2005, 09:42 AM
  5. rand()
    By serious in forum C Programming
    Replies: 8
    Last Post: 02-15-2002, 01:07 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21