switch design

This is a discussion on switch design within the C++ Programming forums, part of the General Programming Boards category; Hello I'm unsure which is the best design when using switch/case. At the moment I do: Code: switch (somecase) { ...

  1. #1
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630

    switch design

    Hello

    I'm unsure which is the best design when using switch/case.

    At the moment I do:

    Code:
    switch (somecase) {
    	default:
    	case 1:
    		{
    			do_something();
    			break;
    		}
    	case 2:
    		{
    			print("blah");
    			break;
    		}
    }
    Should I do break outside the brackets?

  2. #2
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    I don't see why. I do it exactly this way, using the brackets as you do to create a seperates scope

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I would put the break at the outside of the braces (brackets), yes.

    I would also only use braces if I "need them", e.g. there's local variables in that case. Other cases don't actually need braces, so why use them [this is different to using braces on if/while statements, where a missing pair of braces will cause a bug. If you haven't got braces in a case-statement that needs them, the compiler will let you know that in no uncertain terms (or at least, it will give you an error of some sort).

    It is of course a style issue on both accounts - someone else may well say differently, and any larger organization that does software engineering will have some sort of coding guidelines that are more or less strictly enforced. These may tell you how to do case-statements, as well as many other things.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Should I do break outside the brackets?
    Braces aren't required in case statements unless you actually need a separate scope.

    Code:
    BTW, these are brackets:  [ ]
    these are braces:  { }
    and parenthesis:  ( )

  5. #5
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630
    Thanks guys, you're very helpful!

  6. #6
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    It doesn't matter where you break, really. Whether you break within or without the braces it will break from the switch ... but that said I normally only use scoping with cases when I need to, unless it's a longun in which case I use it to show the scope clearly ... and I would break outside ... but does it matter? I don't think so. Though if you do decide to define a scope with the braces it is, IMO, 'nicer' to break outside of it, i.e. when the scope ends.

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, 09:35 AM
  2. ascii rpg help
    By aaron11193 in forum C Programming
    Replies: 18
    Last Post: 10-29-2006, 01:45 AM
  3. Implementing Inheritence into your design
    By bobthebullet990 in forum C++ Programming
    Replies: 6
    Last Post: 08-05-2006, 05:40 PM
  4. Switch
    By cogeek in forum C Programming
    Replies: 4
    Last Post: 12-23-2004, 06:40 PM

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