Thread: switch statement

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    38

    switch statement

    Hello.

    I was wondering if it is "healthy" to define return statements in each switch statement, I mean:

    Code:
    int function (...)
    {
     	int data;
            ...
    	switch (option)
    	{
    		case 'A':   return (data+1)
    				break;
    ...
    		case 'Z':   return (data+26);
    				break;
    	}
    }
    It seems there is no need to use break...?

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    What you could do is have an extra variable to be your return value. In the switch, you can set the return value there, and then return after the switch.

  3. #3
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Well MacGyver's idea would work fine, but really you don't even need an extra variable, just do something like this:

    Code:
    int function (...)
    {
     	int data;
            ...
    	switch (option)
    	{
    		case 'A':   data += 1;
    				break;
    ...
    		case 'Z':   data += 26;
    				break;
    	}
            return(data);
    }
    Personally i'd consider this better style than what you are doing now, and it's not harder to read or understand either...
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    In this particular case, going by the pattern presented, my inclination would be to get rid of the switch entirely.
    Code:
    return data + (option - 'A' + 1);
    (Of course noting possible character set issues.)

    As far as switch cases, I have no issue using a return (and not needing a break after the return). The "problem" that the single point of return argument is supposed to solve tends to confuse me more than this returning "early" that I find simpler. YMMV.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    38
    Thanks for your answer.

    I would like to know if it's just matter of style or it is wrong to do it this way (my way)?

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    IMO: purely style.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

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. char switch statement
    By jmarsh56 in forum C++ Programming
    Replies: 7
    Last Post: 05-03-2006, 05:04 PM
  4. switch statement issues...(undeclared identifiers)
    By mero24 in forum C++ Programming
    Replies: 2
    Last Post: 02-19-2005, 08:05 PM
  5. Efficiency with the switch() statement...
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 10-14-2001, 02:47 PM