Ask about switch

This is a discussion on Ask about switch within the C Programming forums, part of the General Programming Boards category; Please help me. I have code as following Code: #include <stdio.h> int main() { int num = 1; switch (num) ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    2

    Ask about switch

    Please help me. I have code as following

    Code:
    #include <stdio.h>
    
    int main()
    {
    	int num = 1;
    		switch (num)
    		{
    		case 1, 3, 5://causes error - (1)
    			printf("Even");
    			break;
    		case 6..7://causes error - (2)
    			printf("Unknown");
    			break;
    		default:
    			printf("Ok");
    		}
    	return 0;
    }
    Why (1) and (2) cause errors. In switch of C, doesn't support multiple const and interval constant ?

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,650
    cases must be listed separately:
    Code:
    switch (num)
    		{
    		case 1:
                    case 3:
                    case 5://causes error - (1)
    			printf("Even");
    			break;
    		case 6:
                    case 7://causes error - (2)
    			printf("Unknown");
    			break;
    		default:
    			printf("Ok");
    		}
    and no it does not support intervals like that (although that's not even really a valid interval)

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,540
    GCC has case intervals IIRC, but it is very much a compiler extension.

    If you use it, youŽll be tying yourself to a single compiler.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    2
    So bad. If I want to put in integer number, and check if it equals 1, 3, 5, 7, 8, 10, 12 -> printf("has 31 days")... I mean use many case statement. So bad for C and C++.

    Quote Originally Posted by rags_to_riches View Post
    cases must be listed separately:
    Code:
    switch (num)
    		{
    		case 1:
                    case 3:
                    case 5://causes error - (1)
    			printf("Even");
    			break;
    		case 6:
                    case 7://causes error - (2)
    			printf("Unknown");
    			break;
    		default:
    			printf("Ok");
    		}
    and no it does not support intervals like that (although that's not even really a valid interval)

  5. #5
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,701
    Write it with if -- the other kind of branching logic -- and see if it makes more sense to you, I guess.

  6. #6
    Registered User
    Join Date
    Oct 2010
    Location
    Vancouver
    Posts
    5
    It looks to me like you want to print how many days exist in each month. Just setup and array with 12 slots...and put array[0] = 30...then its easy. printf(%u\n",array[number]);

    Of course this is assuming that's what you are trying to do...

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by micheal9000 View Post
    So bad. If I want to put in integer number, and check if it equals 1, 3, 5, 7, 8, 10, 12 -> printf("has 31 days")... I mean use many case statement. So bad for C and C++.
    I don't see how you would think this a bad thing... It's more typing for the programmer but the final code from my compiler is pretty compact. switch() has it's place, especially in Windows where it's used very heavily for handling window messages. Call it a convenience for picking multiple branches...

    If you're looking for lists or ranges (a la Pascal) then you need switch()'s ugly stepsister if() ... else if()... in which case you can put any valid conditional statement you like into your branching code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 08:35 AM
  2. ascii rpg help
    By aaron11193 in forum C Programming
    Replies: 18
    Last Post: 10-29-2006, 01:45 AM
  3. Switch
    By cogeek in forum C Programming
    Replies: 4
    Last Post: 12-23-2004, 05:40 PM
  4. Switch Case
    By FromHolland in forum C++ Programming
    Replies: 7
    Last Post: 06-13-2003, 03:51 AM

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