Thread: Switch selection statement

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    11

    Switch selection statement

    Is there any way of testing for a range of values in a switch statement?

    such as:
    Code:
    int main() {
    
        int x = 5;
    
        switch(x) {
            case 4, 5, 6 :
                break;
            default :
                return 1;
        }
        return 0;
    }
    Obviously, that doesn't work. I've looked around and I can't find an example of this in any tutorial.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    im not very sure what ur asking but if ur asking how u combine results in one switch then yea i understand (i think).
    i think if u dont break; then it carries on to the next one and RUNS it.

    Code:
    int main() {
    
        int x = 4;
    
        switch(x) {
            case 4:
                /* with no break; it goes and runs case 5 */
            case 5:
               /* same with this, it goes and runs case 6 */
            case 6:
               /* 4 and 5 can share same code and it stops at 6 */
             break;           
            default :
                return 1;
        }
        return 0;
    }
    i think thats how u do it

    now if its 4, 5 or 6, u put what u want it to run in case 6

    EDIT: yea it works i tried it
    Last edited by rodrigorules; 09-05-2005 at 03:36 PM.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    11
    Ok, thanks for your help.

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, 03:14 PM
  3. Logical error in Switch Statement..
    By gardenair in forum C Programming
    Replies: 4
    Last Post: 05-19-2003, 10:15 PM
  4. help with switch statement?
    By Kristina in forum C++ Programming
    Replies: 10
    Last Post: 05-14-2002, 11:25 AM
  5. Replies: 2
    Last Post: 05-10-2002, 04:16 PM