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 4 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

    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 }

    N Offline
    N Offline
    N a v a n e e t h
    wrote on last edited by
    #2

    dec82 wrote:

    What could cause above error

    goto

    Navaneeth How to use google | Ask smart questions

    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 }

      D Offline
      D Offline
      DaveyM69
      wrote on last edited by
      #3

      I'm almost speechless! I'll leave it for others to say as I'm in a good mood today. Look up break (and maybe default: too).

      Dave
      BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
      Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
      Why are you using VB6? Do you hate yourself? (Christian Graus)

      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 }

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

        YEUCK! I have copied your code to "Coding Horrors"! This is C#, not C++. Switch blocks cannot fall through in C#, they must end in a break statement. Please, go and look at a C# book under two sections: "switch statement" and "Don't use goto unless you really, really have to"

        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

        R M 2 Replies 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 }

          D Offline
          D Offline
          dec82
          wrote on last edited by
          #5

          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 OriginalGriffO L 3 Replies Last reply
          0
          • OriginalGriffO OriginalGriff

            YEUCK! I have copied your code to "Coding Horrors"! This is C#, not C++. Switch blocks cannot fall through in C#, they must end in a break statement. Please, go and look at a C# book under two sections: "switch statement" and "Don't use goto unless you really, really have to"

            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

            R Offline
            R Offline
            riced
            wrote on last edited by
            #6

            OriginalGriff wrote:

            I have copied your code to "Coding Horrors"!

            You beat me to it! :laugh:

            Regards David R --------------------------------------------------------------- "Every program eventually becomes rococo, and then rubble." - Alan Perlis

            1 Reply Last reply
            0
            • 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
              Lutoslaw
              wrote on last edited by
              #7

              Good question. This code is very C++ - specific. Unfortunately I haven't time to answer you now, but I'm looking forward for someone to provide answer.

              Greetings - Jacek Gajek

              1 Reply Last reply
              0
              • OriginalGriffO OriginalGriff

                YEUCK! I have copied your code to "Coding Horrors"! This is C#, not C++. Switch blocks cannot fall through in C#, they must end in a break statement. Please, go and look at a C# book under two sections: "switch statement" and "Don't use goto unless you really, really have to"

                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
                #8

                OriginalGriff wrote:

                Don't use goto unless you really, really have to

                Fixed that for you :)

                Never underestimate the power of human stupidity RAH

                OriginalGriffO 1 Reply Last reply
                0
                • 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; }

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

                  Firstly, find the programmer who originally wrote this. Take him outside, and beat him senseless. It's pretty bad code as C++, and can't be directly translated into C# as the later enforces rules to prevent accidental mistakes (such as fall through of cases). Secondly, get out a pen and paper and do a flow diagram of some sort to work out how this spagetti works. Tidy the diagram up so it can be reliably implemented in any language. Re-code into C#. Not a quick job by any means, but it's the companies' (or whoevers') fault for allowing such rubbish to be produced in the first place!

                  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

                  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 }

                    0 Offline
                    0 Offline
                    0x3c0
                    wrote on last edited by
                    #10

                    Oh, dear

                    1. Never use goto. It kills babies
                    2. You just drop though each case. Why?
                    3. You do a switch, when you've just set the variable you switch on
                    4. You have one hundred hard-coded cases. Find out how to make that dynamic
                    5. The switch is unnecessary. If you're just dropping through, then it won't make any difference
                    6. You have basically implemented line labels. Why do you have line labels, when you can refactor your code
                    7. No default: option
                    8. The first line of is missing a semicolon
                    L 1 Reply Last reply
                    0
                    • 0 0x3c0

                      Oh, dear

                      1. Never use goto. It kills babies
                      2. You just drop though each case. Why?
                      3. You do a switch, when you've just set the variable you switch on
                      4. You have one hundred hard-coded cases. Find out how to make that dynamic
                      5. The switch is unnecessary. If you're just dropping through, then it won't make any difference
                      6. You have basically implemented line labels. Why do you have line labels, when you can refactor your code
                      7. No default: option
                      8. The first line of is missing a semicolon
                      L Offline
                      L Offline
                      Luc Pattyn
                      wrote on last edited by
                      #11

                      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.


                      OriginalGriffO P M 3 Replies Last reply
                      0
                      • M Mycroft Holmes

                        OriginalGriff wrote:

                        Don't use goto unless you really, really have to

                        Fixed that for you :)

                        Never underestimate the power of human stupidity RAH

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

                        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

                        "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

                        D M 2 Replies 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

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

                          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 P 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.


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

                            Luc Pattyn wrote:

                            If C# had a "computed goto"

                            I'd have stuck with C++. :laugh:

                            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

                              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
                                          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