Equivalent of less than in a switch statement?

This is a discussion on Equivalent of less than in a switch statement? within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm looking for an equivalent for a < or > in a switch statement. IE: switch (number){ case 10 ...

  1. #1
    Registered User Diamonds's Avatar
    Join Date
    Oct 2002
    Posts
    68

    Question Equivalent of less than in a switch statement?

    Hi, I'm looking for an equivalent for a < or > in a switch statement.
    IE:

    switch (number){

    case 10 <number< 20: blah;
    break;
    case number >20: bleh;
    break;
    default: bleg;
    break;
    }

    anyone know how to do this?

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    You can use if/else statements. Like the code just put here.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User Diamonds's Avatar
    Join Date
    Oct 2002
    Posts
    68

    yep

    I know this, but I would have sworn that there was a work around. (that I have compeletely forgotten)

    I really don't want to start changing all my code around =/

  4. #4
    Registered User Diamonds's Avatar
    Join Date
    Oct 2002
    Posts
    68
    i tried searching too... didn't help because I couldn't think of any great key words that would find the answer

  5. #5
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Code:
    switch (num)
    {
          case 1:
          case 2:
          case 3:
          case 4: { cout <<"Less than five";break;}
          
          case 5:
          case 6:
          case 7:
          case 8:
          case 9: { cout<<"Less than ten";break;}
          default: cout<<"Not single digit num";
    }
    Thats about the best you can do with a switch for this sort of thing. You would probably be better using a different structure. If/else can be useful but be careful not to get elseifheimers disease.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  6. #6
    Registered User Diamonds's Avatar
    Join Date
    Oct 2002
    Posts
    68
    figured, just thought I'd ask....

    thnx all

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mutli Switch statement help
    By elwad in forum C Programming
    Replies: 9
    Last Post: 05-09-2009, 03:19 AM
  2. Switch statement / default:
    By kcpilot in forum C Programming
    Replies: 4
    Last Post: 12-02-2008, 02:14 PM
  3. Switch statement problem
    By jalex39 in forum C Programming
    Replies: 6
    Last Post: 03-08-2008, 03:05 PM
  4. The switch statement when comparing a character
    By MiamiCuse in forum C Programming
    Replies: 5
    Last Post: 11-04-2005, 01:26 AM
  5. Efficiency with the switch() statement...
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 10-14-2001, 02:47 PM

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