Thread: Switch Case

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    32

    Switch Case

    In a switch statement is it possible to do something like this in c
    Code:
    	result = -1; 
     switch ( result ) {
      case 1:
        printf("FILENAME ERROR #:%d -Partial match; the name fits the left-hand pattern sections.  ", result);
      break;
      case 0:
        printf("FILENAME ERROR #:%d -Partial match; No match; the name does not fit the pattern.  ", result);
      break;
      case -1,-2:
        printf("FILENAME ERROR #:%d -An error occurred, CHECK PARAMETERS.  ", result);
      break;
      }
    or even this:

    Code:
    	result = -1; 
     switch ( result ) {
      case 1:
        printf("FILENAME ERROR #:%d -Partial match; the name fits the left-hand pattern sections.  ", result);
      break;
      case 0:
        printf("FILENAME ERROR #:%d -Partial match; No match; the name does not fit the pattern.  ", result);
      break;
      case < 0 :
        printf("FILENAME ERROR #:%d -An error occurred, CHECK PARAMETERS.  ", result);
      break;
      }
    Because for both attempts don't work in c i was wondering if there is a different syntax for it.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You want an if / else chain for that probably. You can do this though:
    Code:
    switch( x )
    {
        case 1:
        case 2: /* no break at the end of case 1: drops through to case 2... */
        break;
    
        default: /* everything else */
    }
    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    32
    thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help me with switch case!
    By blakjakd in forum C++ Programming
    Replies: 8
    Last Post: 06-24-2010, 08:20 AM
  2. Replies: 11
    Last Post: 08-25-2008, 12:01 PM
  3. Switch-case help
    By devilboybf in forum C++ Programming
    Replies: 7
    Last Post: 06-19-2005, 11:46 PM
  4. Switch/Case using && != and ||?
    By Kespoosh in forum C++ Programming
    Replies: 6
    Last Post: 03-15-2003, 11:24 PM
  5. switch case
    By threahdead in forum C Programming
    Replies: 5
    Last Post: 09-30-2002, 02:37 PM