Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Control cannot fall through from one case to another?

Control cannot fall through from one case to another?

Scheduled Pinned Locked Moved C#
helpquestion
35 Posts 15 Posters 3 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D dec82

    I need to convert these C++ code into C# code . What's is the easiest way ? thanks state=1 switch (state) { case 1: //do something state++; case 2: //do something if ( ) { state++; } else { state=state+5; } break; case 3: //do something state++; ............ case 100: break; }

    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #15

    Why use a case at all, it seems pretty procedural to me. You want to execute a block of code in order, your condition isn't really needed since you always increment 'state', so your select case is useless.

    Check out the CodeProject forum Guidelines[^]

    L 1 Reply Last reply
    0
    • D Dan Neely

      If you're a student/junior dev though; the rule is NEVER. You're not experienced enough to ID the few cases where it really is the best choice and will just learn bad habits.

      It is a truth universally acknowledged that a zombie in possession of brains must be in want of more brains. -- Pride and Prejudice and Zombies

      N Offline
      N Offline
      Nagy Vilmos
      wrote on last edited by
      #16

      dan neely wrote:

      the rule is NEVER

      Absolutly true... Learn the law! Obey the law! Fear the law! Believe the law! And THEN you can break the law!


      Panic, Chaos, Destruction. My work here is done.

      OriginalGriffO 1 Reply Last reply
      0
      • D Dan Neely

        If you're a student/junior dev though; the rule is NEVER. You're not experienced enough to ID the few cases where it really is the best choice and will just learn bad habits.

        It is a truth universally acknowledged that a zombie in possession of brains must be in want of more brains. -- Pride and Prejudice and Zombies

        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #17

        Then how do you execute multiple cases in a switch in C#?

        D 1 Reply Last reply
        0
        • D dec82

          hi I have these code . What could cause above error ? Thanks state=1 switch (state) { case 1: //do something goto case 2; case 2: //do something goto case 3; case 3: //do something goto case 4; ............ case 100: //do something }

          P Offline
          P Offline
          Paulo Zemek
          wrote on last edited by
          #18

          I wrote this code to test: int i=int.Parse(Console.ReadLine()); switch(i) { case 0: Console.WriteLine(0); goto case 1; case 1: Console.WriteLine(1); break; } This works. If you don't do the goto case 1 you will receive an error, and if you don't do the break at case 1 you will also receive an error. I think the problem with your code is the lack of some "goto case x" or some "break". --- I don't like such gotos, but if that the best way, try finding the lacking gotos / breaks.

          modified on Wednesday, May 13, 2009 10:06 AM

          P L 2 Replies Last reply
          0
          • L Luc Pattyn

            Computafreak wrote:

            You just drop though each case. Why?

            This is a state-machine with a selectable first state. If C# had a "computed goto" (as Fortran has) then the switch would not be necessary. :)

            Luc Pattyn [Forum Guidelines] [My Articles]


            The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


            P Offline
            P Offline
            PIEBALDconsult
            wrote on last edited by
            #19

            Luc Pattyn wrote:

            state-machine

            I prefer to put the switch in a while, rather than use fall-through for that.

            1 Reply Last reply
            0
            • N Nagy Vilmos

              dan neely wrote:

              the rule is NEVER

              Absolutly true... Learn the law! Obey the law! Fear the law! Believe the law! And THEN you can break the law!


              Panic, Chaos, Destruction. My work here is done.

              OriginalGriffO Offline
              OriginalGriffO Offline
              OriginalGriff
              wrote on last edited by
              #20

              williamnw wrote:

              Learn the law! Obey the law! Fear the law! Believe the law!

              **

              Judge Dredd wrote:

              I am the law!!

              **

              No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones

              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
              "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

              1 Reply Last reply
              0
              • P Paulo Zemek

                I wrote this code to test: int i=int.Parse(Console.ReadLine()); switch(i) { case 0: Console.WriteLine(0); goto case 1; case 1: Console.WriteLine(1); break; } This works. If you don't do the goto case 1 you will receive an error, and if you don't do the break at case 1 you will also receive an error. I think the problem with your code is the lack of some "goto case x" or some "break". --- I don't like such gotos, but if that the best way, try finding the lacking gotos / breaks.

                modified on Wednesday, May 13, 2009 10:06 AM

                P Offline
                P Offline
                Paulo Zemek
                wrote on last edited by
                #21

                But remember that the goto in this case is fixed. If you need something like: switch(x) { case 0: if (someCondition) x += 5; else x += 6 do other processing, and then continue with the next case for X (that can be 5, 6 or some other value) the best solution will be to use a while. For example: bool continueRunning = true; while (continueRunning) { switch(x) { case 0: // do something; if (someCondition) x += 5; else x += 6; break; ... other cases ... default: continueRunning = false; break; } } So, this will: Execute the switch with x being zero. You can then recalculate x, and it will execute the switch again. You can do that how many times you want. If you want to stop it, you call continueRunning = false; And, if the value does not fall in any case, it will enter the default, with will set continueRunning to false and stops the block.

                modified on Wednesday, May 13, 2009 10:07 AM

                1 Reply Last reply
                0
                • P Paulo Zemek

                  I wrote this code to test: int i=int.Parse(Console.ReadLine()); switch(i) { case 0: Console.WriteLine(0); goto case 1; case 1: Console.WriteLine(1); break; } This works. If you don't do the goto case 1 you will receive an error, and if you don't do the break at case 1 you will also receive an error. I think the problem with your code is the lack of some "goto case x" or some "break". --- I don't like such gotos, but if that the best way, try finding the lacking gotos / breaks.

                  modified on Wednesday, May 13, 2009 10:06 AM

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #22

                  Paulo Zemek wrote:

                  This works.

                  Yes. So does this:

                  Console.WriteLine(0);
                  Console.WriteLine(1);

                  Same outcome, more readable though. :)

                  Luc Pattyn [Forum Guidelines] [My Articles]


                  The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


                  P 1 Reply Last reply
                  0
                  • L Luc Pattyn

                    Paulo Zemek wrote:

                    This works.

                    Yes. So does this:

                    Console.WriteLine(0);
                    Console.WriteLine(1);

                    Same outcome, more readable though. :)

                    Luc Pattyn [Forum Guidelines] [My Articles]


                    The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


                    P Offline
                    P Offline
                    Paulo Zemek
                    wrote on last edited by
                    #23

                    This does not generate the same result. I set the i = 0. But consider that i is entered by the user. Console.WriteLine(0); Console.WriteLine(1); will not be the same, as in some cases only the case 1 must be executed. As I understand, the idea is to have the option to "start at any point", but continue from it. And I am not saying the solution used is a good one, but it is the easiest to convert a C++ code without really refactoring it.

                    OriginalGriffO 1 Reply Last reply
                    0
                    • P PIEBALDconsult

                      Then how do you execute multiple cases in a switch in C#?

                      D Offline
                      D Offline
                      Dan Neely
                      wrote on last edited by
                      #24

                      LalalalaICantHearYouLalalalalala Outside of a single school exercise (Generate the complete lyrics for the 12 days of Christmas) I can't think of a single case where falling through was desired behavior. Seriously though, that's the exception which proves the rule. :-D

                      It is a truth universally acknowledged that a zombie in possession of brains must be in want of more brains. -- Pride and Prejudice and Zombies

                      M P 2 Replies Last reply
                      0
                      • D Dan Neely

                        LalalalaICantHearYouLalalalalala Outside of a single school exercise (Generate the complete lyrics for the 12 days of Christmas) I can't think of a single case where falling through was desired behavior. Seriously though, that's the exception which proves the rule. :-D

                        It is a truth universally acknowledged that a zombie in possession of brains must be in want of more brains. -- Pride and Prejudice and Zombies

                        M Offline
                        M Offline
                        molesworth
                        wrote on last edited by
                        #25

                        dan neely wrote:

                        Outside of a single school exercise (Generate the complete lyrics for the 12 days of Christmas) I can't think of a single case where falling through was desired behavior.

                        It does happen occasionally. My current project has two switches where it was much clearer to add a "goto" rather than convoluted logic. The important thing, of course, is to comment it properly so everyone can see exactly what's being done, and why.

                        There are three kinds of people in the world - those who can count and those who can't...

                        1 Reply Last reply
                        0
                        • L Luc Pattyn

                          Computafreak wrote:

                          You just drop though each case. Why?

                          This is a state-machine with a selectable first state. If C# had a "computed goto" (as Fortran has) then the switch would not be necessary. :)

                          Luc Pattyn [Forum Guidelines] [My Articles]


                          The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


                          M Offline
                          M Offline
                          molesworth
                          wrote on last edited by
                          #26

                          Luc Pattyn wrote:

                          This is a state-machine with a selectable first state. If C# had a "computed goto" (as Fortran has) then the switch would not be necessary.

                          Or do the state machine as a "while...switch..." loop, and set the initial state before entry. That's how I've always done it (and I do go back to the old Fortran days as well :)).

                          There are three kinds of people in the world - those who can count and those who can't...

                          1 Reply Last reply
                          0
                          • L Lost User

                            Why use a case at all, it seems pretty procedural to me. You want to execute a block of code in order, your condition isn't really needed since you always increment 'state', so your select case is useless.

                            Check out the CodeProject forum Guidelines[^]

                            L Offline
                            L Offline
                            Lutoslaw
                            wrote on last edited by
                            #27

                            I'm back. It depends on what state is equal to. As I have understood, the state=1 line is just an example of possible value which can take state. If state would be fixed to 1 every time, 80% of could be removed, including the whole switch statement. So the question to dec82: Is state=1 line just an example or is it actually present in the source code?

                            Greetings - Jacek Gajek

                            OriginalGriffO 1 Reply Last reply
                            0
                            • D Dan Neely

                              LalalalaICantHearYouLalalalalala Outside of a single school exercise (Generate the complete lyrics for the 12 days of Christmas) I can't think of a single case where falling through was desired behavior. Seriously though, that's the exception which proves the rule. :-D

                              It is a truth universally acknowledged that a zombie in possession of brains must be in want of more brains. -- Pride and Prejudice and Zombies

                              P Offline
                              P Offline
                              PIEBALDconsult
                              wrote on last edited by
                              #28

                              I agree that fall-through is evil, but there are still situations where you want to execute multiple cases; if not with goto, then some other keyword, at least the goto acts as a cry for help.

                              1 Reply Last reply
                              0
                              • L Lutoslaw

                                I'm back. It depends on what state is equal to. As I have understood, the state=1 line is just an example of possible value which can take state. If state would be fixed to 1 every time, 80% of could be removed, including the whole switch statement. So the question to dec82: Is state=1 line just an example or is it actually present in the source code?

                                Greetings - Jacek Gajek

                                OriginalGriffO Offline
                                OriginalGriffO Offline
                                OriginalGriff
                                wrote on last edited by
                                #29

                                Not necessarily. As Luc said earlier, this looks like a state machine implemented as a single switch block. As such, it may never exit! It could be accepting inputs from (say) Console.Readline and modifying state as a result. I've seen it done (by crap proceduraly oriented programmers) before.

                                No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones

                                "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                                "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                                L 1 Reply Last reply
                                0
                                • P Paulo Zemek

                                  This does not generate the same result. I set the i = 0. But consider that i is entered by the user. Console.WriteLine(0); Console.WriteLine(1); will not be the same, as in some cases only the case 1 must be executed. As I understand, the idea is to have the option to "start at any point", but continue from it. And I am not saying the solution used is a good one, but it is the easiest to convert a C++ code without really refactoring it.

                                  OriginalGriffO Offline
                                  OriginalGriffO Offline
                                  OriginalGriff
                                  wrote on last edited by
                                  #30

                                  Yeah. And then some poor sod (probably me with any luck) gets to maintain the ramshackle heap of spagetti. Do the job right. If you are converting C++ to C# then convert it to good C#. It may be you who has to "upgrade" it in six months time...

                                  No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones

                                  "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                                  "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                                  1 Reply Last reply
                                  0
                                  • D dec82

                                    hi I have these code . What could cause above error ? Thanks state=1 switch (state) { case 1: //do something goto case 2; case 2: //do something goto case 3; case 3: //do something goto case 4; ............ case 100: //do something }

                                    L Offline
                                    L Offline
                                    Lutoslaw
                                    wrote on last edited by
                                    #31

                                    Take a look at my article on FSA. An Object-oriented Approach to Finite State Automata[^]. Hope this helps.

                                    Greetings - Jacek Gajek

                                    1 Reply Last reply
                                    0
                                    • OriginalGriffO OriginalGriff

                                      Not necessarily. As Luc said earlier, this looks like a state machine implemented as a single switch block. As such, it may never exit! It could be accepting inputs from (say) Console.Readline and modifying state as a result. I've seen it done (by crap proceduraly oriented programmers) before.

                                      No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones

                                      L Offline
                                      L Offline
                                      Lutoslaw
                                      wrote on last edited by
                                      #32

                                      Kinda cool although unmaintainable a bit. I've posted him a link to my FSA stuff.

                                      Greetings - Jacek Gajek

                                      OriginalGriffO 1 Reply Last reply
                                      0
                                      • L Lutoslaw

                                        Kinda cool although unmaintainable a bit. I've posted him a link to my FSA stuff.

                                        Greetings - Jacek Gajek

                                        OriginalGriffO Offline
                                        OriginalGriffO Offline
                                        OriginalGriff
                                        wrote on last edited by
                                        #33

                                        Jacek Gajek wrote:

                                        Kinda cool although unmaintainable a bit.

                                        Trust me, I'm not defending this code at all - I posted it to "Coding Horrors" - I like the understatement though! ;)

                                        No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones

                                        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                                        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                                        1 Reply Last reply
                                        0
                                        • OriginalGriffO OriginalGriff

                                          There are times when a goto can really improve readability, rather than if...if..if..if... etc. Having said that, I don't think I've used one in a non-assembler language for twenty or so years.

                                          No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones

                                          M Offline
                                          M Offline
                                          Mycroft Holmes
                                          wrote on last edited by
                                          #34

                                          OriginalGriff wrote:

                                          goto can really improve

                                          Nope - never happenned in, yeah probably 15 years. It never even enters the thought processes as a tool to solve a problem, and I started programming VBA macros way back. If one of my juniors devs used a goto I'd attack him with a bar of soap, or super glue the g key to his forehead.

                                          Never underestimate the power of human stupidity RAH

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups