Thread: using ranged values with the switch flow control statement

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    10

    Thumbs up using ranged values with the switch flow control statement

    I have a doubt, is there any way to use ranges instead of specific values in a switch statemnt? i.e. how can i do something like:

    Code:
    switch(var){
       case 0 to 100:
         /*Some action*/
         break;
       case 101 to 200:
        /*Something else*/ 
        break;
       default:
         /*Yet something else*/
         break;
    }
    I know it is possible in other languages but I have looked into several C books and online articles and haven't found one that talks about this.

    Many thanks!

  2. #2
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215
    Never seen it done or heard of it either. Just use an If statment.
    -"What we wish, we readily believe, and what we ourselves think, we imagine others think also."
    PHP Code:
    sadf 

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    in C language you have to use if conditions
    Code:
    if( var >= 0 && var <= 100)
    {
      // do something
    }
    else if( var >= 101 && var <= 200)
    {
      // do something
    }

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You don't have to...
    Code:
    switch( num )
    {
        case  0:case  1:case  2:case  3:case  4:case  5:case  6:case  7:case  8:case  9:
        case 10:case 11:case 12:case 13:case 14:case 15:case 16:case 17:case 18:case 19:
        case 20:case 21:case 22:case 23:case 24:case 25:case 26:case 27:case 28:case 29:
        case 30:case 31:case 32:case 33:case 34:case 35:case 36:case 37:case 38:case 39:
        case 40:case 41:case 42:case 43:case 44:case 45:case 46:case 47:case 48:case 49:
        case 50:case 51:case 52:case 53:case 54:case 55:case 56:case 57:case 58:case 59:
        case 60:case 61:case 62:case 63:case 64:case 65:case 66:case 67:case 68:case 69:
        case 70:case 71:case 72:case 73:case 74:case 75:case 76:case 77:case 78:case 79:
        case 80:case 81:case 82:case 83:case 84:case 85:case 86:case 87:case 88:case 89:
        case 90:case 91:case 92:case 93:case 94:case 95:case 96:case 97:case 98:case 99:
        case 100:
            /* your code here */
        break;
    
        /* The rest is up to you. ;) */
    
    }



    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    10
    I think I like the if option better. Thank you!

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Everyone's a critic...


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    quzah was just teasing

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Switch statement / default:
    By kcpilot in forum C Programming
    Replies: 4
    Last Post: 12-02-2008, 03:14 PM
  2. Getting the switch statement to work.
    By mtymightymike in forum C Programming
    Replies: 7
    Last Post: 10-15-2008, 06:32 PM
  3. Stack operations from switch statement elements
    By mlsrar in forum C Programming
    Replies: 15
    Last Post: 10-02-2008, 01:12 PM
  4. Error proofing my switch statement.
    By Shamino in forum C++ Programming
    Replies: 10
    Last Post: 01-04-2008, 04:38 PM
  5. Checking Multiple Values in if() statement
    By Zero_X in forum C++ Programming
    Replies: 6
    Last Post: 10-02-2006, 11:27 PM