switch question

This is a discussion on switch question within the C++ Programming forums, part of the General Programming Boards category; I have a simple switch: Code: int sumnum = 3; switch(sumnum) { case 1: break; case 2: dosth(); break; case ...

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

    switch question

    I have a simple switch:

    Code:
    int sumnum = 3;
    switch(sumnum) {
    	case 1:
    		break;
    	case 2:
    		dosth();
    		break;
    	case 3:
    		//will it preform action here - yes
    	case 3:
    	case 4:
    		//will it go here?
    		break;		
    }
    So if I pass number 3 to switch, will it preform actions on case 3, what about case 3, 4?

  2. #2
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630
    I just figgured this wouldnt even allow me to compile..

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Just out of curiosity, why do you have two of the same case at all?
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2,010
    Your case 1 is useless. the switch would work the same without. Having two case 3 probably won't compile. if it does, it will simply ignore the second. But there is no break after case 3, so it will continue doing the next case untill it encounters a break or exits the switch.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  5. #5
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Your case 1 is useless.
    That's a prototype.

    Duplicate cases are illegal. If you want case 4 to run too when case 3 activates, then it does what you want.
    But it seems kind of strange way to me.
    Last edited by maxorator; 11-08-2006 at 08:49 AM.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  6. #6
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630
    I wanted to make less code and look better..

    I needed two cases do the same thing, except one do something more..

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I needed two cases do the same thing, except one do something more..
    Read that a few times, please. It's completely nonsensical.
    My best code is written with the delete key.

  8. #8
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    Code:
    int sumnum = 3;
    
    switch(sumnum) 
    {
        case 1:
            // Do something for case 1 only
            break;
        case 2:
            // Do something for case 2 only
            break;
        case 3:
            // Do something for case 3 only
            // drop through deliberately
        case 4:
            // Do something for case 3 and case 4
            break;		
        default:
            // Do something for default only
            break;
    };
    Is that what you meant?

  9. #9
    Registered User
    Join Date
    Apr 2006
    Posts
    2,010
    Quote Originally Posted by l2u
    I wanted to make less code and look better..

    I needed two cases do the same thing, except one do something more..
    The you do this:
    Code:
    switch(sumnum) {
    	case 1:
                    //put code here
    		break;
    	case 2:
    		dosth();
    		break;
    	case 3:
    		//do if sumnum=3
    	case 4:
    		//do if sumnum=4, or after doing condition for sumnum=3.
    		break;		
    }
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  10. #10
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    Forgetting to place a break after each case will only cause it to "fall through" to the next one anyway. I always make sure I end my cases with a break, it makes good sense, unless of course the idea of the program is to test the weakness in a switch statement that is
    I'm just trying to be a better person - My Name Is Earl

  11. #11
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,383
    However, as King Mir shown, not always placing a break is what is intended.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  12. #12
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    EDIT:
    > i wanted the same case to do the same thing

    In that case, leave out the break in one of the cases, eg, case 3 and it will execute the second
    after it has fallen through the first.
    Does sound rather odd though...
    I'm just trying to be a better person - My Name Is Earl

  13. #13
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630
    Is that smart to do? Would you pros do that?

  14. #14
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,383
    Of course they would. Why do you think the switch semantics include the ability to fall through different case statements?

    EDIT: However, as a second thought, if your switch expression is built around the use of breaks and only one or another case statement doesn't have it, it doesn't hurt to place a comment reminding you and anyone else who reads your code, that is really intended.
    Last edited by Mario F.; 11-08-2006 at 09:58 AM.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  15. #15
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    But if you are going to deliberately fall through different case statements, it's advisable to clearly comment it so that at a later date someone maintaining your code does "correct" your "mistake" inadvertently.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  2. Switch Case Question
    By dnysveen in forum C++ Programming
    Replies: 12
    Last Post: 06-06-2006, 05:34 AM
  3. Replies: 7
    Last Post: 05-25-2006, 12:51 PM
  4. Exam Question - Possible Mistake?
    By Richie T in forum C++ Programming
    Replies: 15
    Last Post: 05-08-2006, 03:44 PM
  5. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 02:57 PM

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