switch statements with greater/less than operators

This is a discussion on switch statements with greater/less than operators within the C++ Programming forums, part of the General Programming Boards category; How would I create a switch statement that would have cases such as 'x < r' or something like that ...

  1. #1
    _ Munkey01's Avatar
    Join Date
    Dec 2002
    Posts
    280

    Question switch statements with greater/less than operators

    How would I create a switch statement that would have cases such as 'x < r' or something like that if x is the switch parameter?

    Something like-
    Code:
    char x=10;
    switch(x)
    {
            case > 1: cout << "x is greater than 1"; break;
            case < 1: cout << "x is less than 1"; break;
            default: cout << "Other..."; break;
    }
    ?

  2. #2
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>How would I create a switch statement that would have cases such as 'x < r' or something like that if x is the switch parameter?
    You can't, switch cases have to be integral literals. Use an if/else series instead :-)

    EDIT:
    Or you can use a true/false switch if that's all you want
    Code:
    switch (x > 1)
    {
    case true: // Something
    case false: // Something else
    }
    *Cela*

  3. #3
    _ Munkey01's Avatar
    Join Date
    Dec 2002
    Posts
    280
    I don't think the true/false statement would work in my case. Just in my sloppy code if/else if statements just make the code look that much worse.

  4. #4
    Registered User abrege's Avatar
    Join Date
    Nov 2002
    Posts
    369
    Using the ternary operator you could do a one-liner and perhaps not sacrifice the purtiness of your code...

    Code:
    (x > 1 ? cout << "greater that one" : cout << "less than one");
    I am against the teaching of evolution in schools. I am also against widespread
    literacy and the refrigeration of food.

  5. #5
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    You could expand on both replies

    Code:
    #include <iostream>
    
    using namespace std; //introduces namespace std
    
    int main ( void )
    {
       int x=5, y=8;
    	
       switch(x > y ? true : false) // or 1 : 0
       {
          case true:  cout << x 
                           << " is greater than "
                           << y;
              
                           break;
    		
          case false: cout << y
                           << " is greater than "
                           << x;
              
                           break;
       };	
    	
       return 0;
    }

  6. #6
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>switch(x > y ? true : false) // or 1 : 0
    Why do that? Using a relational operator automatically returns true and false, it's redundant to do it explicitly :-)
    *Cela*

  7. #7
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    Dunno... my mind wanders at times since I'm going back and forth between this board, researching for a paper, and checking E-mail. :P
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  8. #8
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    ~

    "How would I create a switch statement that would have cases such as 'x < r' or something like that if x is the switch parameter?"

    Code:
    if (x < r) {
    
    }
    Functionality over visuality.
    "He who makes a beast of himself, gets rid of the pain of being a man." Dr. Johnson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using Files Inside Switch Statements
    By arealfind08 in forum C Programming
    Replies: 11
    Last Post: 03-17-2009, 04:49 PM
  2. Explanation of switch statements
    By ammochck21 in forum C++ Programming
    Replies: 6
    Last Post: 11-04-2006, 01:59 PM
  3. arrays within switch statements
    By divinyl in forum C++ Programming
    Replies: 6
    Last Post: 07-17-2003, 01:56 PM
  4. help with switch statements
    By Wexy in forum C Programming
    Replies: 3
    Last Post: 11-06-2002, 04:44 PM
  5. Switch Statements
    By blackgingr in forum C Programming
    Replies: 3
    Last Post: 10-07-2002, 02:36 PM

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