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. The Lounge
  3. Breaks

Breaks

Scheduled Pinned Locked Moved The Lounge
c++question
40 Posts 22 Posters 6 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.
  • T toxcct

    yes it is very useful sometimes !!! for instance, have a look at VisualCalc's Parser[^], and get into the GetToken() function (VCalcParser.cpp, by the end of the file) ;) I'd even say that without the break, switch has no more interrest than a if statement. but thanks to the fact that we can continue the execution path through the following case statements until a break is found is a particularity we found in no other C/C++ fundamental.


    [VisualCalc][Flags Beginner's Guide] | [Forums Guidelines][My Best Advice]

    E Offline
    E Offline
    Eytukan
    wrote on last edited by
    #4

    Yeah I get it. It'll be needed when the "switched" variable gets modified inside a particular case.. I miss a reswitch -- modified at 22:14 Tuesday 6th February, 2007


    Dario: How is "directory" in French? (I mean a file system directory). John Simmons: "zee file holdaire thingie"

    1 Reply Last reply
    0
    • E Eytukan

      Do you think the "break;" [C++] command is really needed at the end of every case statement? I've never been in a situation where I'd want the control to pass on to the next case after satisfying a particular case. You know I missed to put a "break;" and it resulted in a big chaos :doh:.


      Dario: How is "directory" in French? (I mean a file system directory). John Simmons: "zee file holdaire thingie"

      M Offline
      M Offline
      Michael Dunn
      wrote on last edited by
      #5

      I've probably coded a fall-through like that maybe twice in my life. Not counting those times where I goofed and forgot the break. ;) (That shows what I think is a design mistake in C - the default behavior should be no fall-through between cases, since that's what programmers will want the vast majority of the time. Oh well.) I was just looking up the C# spec and C# requires break statements even though the behavior is no fall-through. Bwa? :confused:

      --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ Ford, what's this fish doing in my ear?

      E C S 4 Replies Last reply
      0
      • M Michael Dunn

        I've probably coded a fall-through like that maybe twice in my life. Not counting those times where I goofed and forgot the break. ;) (That shows what I think is a design mistake in C - the default behavior should be no fall-through between cases, since that's what programmers will want the vast majority of the time. Oh well.) I was just looking up the C# spec and C# requires break statements even though the behavior is no fall-through. Bwa? :confused:

        --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ Ford, what's this fish doing in my ear?

        E Offline
        E Offline
        Eytukan
        wrote on last edited by
        #6

        "Failing to check the breaks would really end up in crashes". Proved right in my case :sigh:


        Dario: How is "directory" in French? (I mean a file system directory). John Simmons: "zee file holdaire thingie"

        1 Reply Last reply
        0
        • E Eytukan

          Do you think the "break;" [C++] command is really needed at the end of every case statement? I've never been in a situation where I'd want the control to pass on to the next case after satisfying a particular case. You know I missed to put a "break;" and it resulted in a big chaos :doh:.


          Dario: How is "directory" in French? (I mean a file system directory). John Simmons: "zee file holdaire thingie"

          V Offline
          V Offline
          Vasudevan Deepak Kumar
          wrote on last edited by
          #7

          The biggest risk of missing a break would make the control fallthrough from one case to another and the elegant feature of C# compiler in overcoming this is to raise a compiler error when it finds such a scenario. However, empty case statements are allowed to fall through though.

          Vasudevan Deepak Kumar Personal Homepage Tech Gossips

          C 1 Reply Last reply
          0
          • M Michael Dunn

            I've probably coded a fall-through like that maybe twice in my life. Not counting those times where I goofed and forgot the break. ;) (That shows what I think is a design mistake in C - the default behavior should be no fall-through between cases, since that's what programmers will want the vast majority of the time. Oh well.) I was just looking up the C# spec and C# requires break statements even though the behavior is no fall-through. Bwa? :confused:

            --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ Ford, what's this fish doing in my ear?

            E Offline
            E Offline
            Eytukan
            wrote on last edited by
            #8

            Michael Dunn wrote:

            I was just looking up the C# spec and C# requires break statements even though the behavior is no fall-through.

            :confused::confused::~


            Dario: How is "directory" in French? (I mean a file system directory). John Simmons: "zee file holdaire thingie"

            1 Reply Last reply
            0
            • E Eytukan

              Do you think the "break;" [C++] command is really needed at the end of every case statement? I've never been in a situation where I'd want the control to pass on to the next case after satisfying a particular case. You know I missed to put a "break;" and it resulted in a big chaos :doh:.


              Dario: How is "directory" in French? (I mean a file system directory). John Simmons: "zee file holdaire thingie"

              C Offline
              C Offline
              Christian Graus
              wrote on last edited by
              #9

              VuNic wrote:

              I've never been in a situation where I'd want the control to pass on to the next case after satisfying a particular case.

              I have, many times. I *hate* that .NET doesn't allow this, and forces me to put a break.

              Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

              realJSOPR R 2 Replies Last reply
              0
              • E Eytukan

                Do you think the "break;" [C++] command is really needed at the end of every case statement? I've never been in a situation where I'd want the control to pass on to the next case after satisfying a particular case. You know I missed to put a "break;" and it resulted in a big chaos :doh:.


                Dario: How is "directory" in French? (I mean a file system directory). John Simmons: "zee file holdaire thingie"

                P Offline
                P Offline
                peterchen
                wrote on last edited by
                #10

                It should be the default, and fallthrough; should be the optional keyword However, this would have introduced another keyword. Maybe one could have reused "continue". I guess the "break" comes from the association of switch/case with goto labels.


                Developers, Developers, Developers, Developers, Developers, Developers, Velopers, Develprs, Developers!
                We are a big screwed up dysfunctional psychotic happy family - some more screwed up, others more happy, but everybody's psychotic joint venture definition of CP
                Linkify!|Fold With Us!

                1 Reply Last reply
                0
                • M Michael Dunn

                  I've probably coded a fall-through like that maybe twice in my life. Not counting those times where I goofed and forgot the break. ;) (That shows what I think is a design mistake in C - the default behavior should be no fall-through between cases, since that's what programmers will want the vast majority of the time. Oh well.) I was just looking up the C# spec and C# requires break statements even though the behavior is no fall-through. Bwa? :confused:

                  --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ Ford, what's this fish doing in my ear?

                  C Offline
                  C Offline
                  Christian Graus
                  wrote on last edited by
                  #11

                  At the summit, I listed several cases like this where C# fights you, and was told that they will continue to design C# for ease of use. I guess the alternative, to assume their users are not stupid, is lost on them.

                  Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

                  1 Reply Last reply
                  0
                  • V Vasudevan Deepak Kumar

                    The biggest risk of missing a break would make the control fallthrough from one case to another and the elegant feature of C# compiler in overcoming this is to raise a compiler error when it finds such a scenario. However, empty case statements are allowed to fall through though.

                    Vasudevan Deepak Kumar Personal Homepage Tech Gossips

                    C Offline
                    C Offline
                    Christian Graus
                    wrote on last edited by
                    #12

                    That's not elegant - it sucks !!!

                    Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

                    1 Reply Last reply
                    0
                    • E Eytukan

                      Do you think the "break;" [C++] command is really needed at the end of every case statement? I've never been in a situation where I'd want the control to pass on to the next case after satisfying a particular case. You know I missed to put a "break;" and it resulted in a big chaos :doh:.


                      Dario: How is "directory" in French? (I mean a file system directory). John Simmons: "zee file holdaire thingie"

                      realJSOPR Offline
                      realJSOPR Offline
                      realJSOP
                      wrote on last edited by
                      #13

                      VuNic wrote:

                      Do you think the "break;" [C++] command is really needed at the end of every case statement?

                      No, it's not needed at the end of every one. There are rare instances where you might want to process code in one case but fall through to the next.

                      VuNic wrote:

                      I've never been in a situation where I'd want the control to pass on to the next case after satisfying a particular case.

                      Then in your case, you need a break between cases.

                      VuNic wrote:

                      You know I missed to put a "break;" and it resulted in a big chaos

                      Silver lining - you've become a better programmer as a result of the experience.

                      "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                      -----
                      "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

                      E 1 Reply Last reply
                      0
                      • C Christian Graus

                        VuNic wrote:

                        I've never been in a situation where I'd want the control to pass on to the next case after satisfying a particular case.

                        I have, many times. I *hate* that .NET doesn't allow this, and forces me to put a break.

                        Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

                        realJSOPR Offline
                        realJSOPR Offline
                        realJSOP
                        wrote on last edited by
                        #14

                        But we're talking about C++ here...

                        "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                        -----
                        "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

                        C 1 Reply Last reply
                        0
                        • realJSOPR realJSOP

                          But we're talking about C++ here...

                          "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                          -----
                          "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

                          C Offline
                          C Offline
                          Christian Graus
                          wrote on last edited by
                          #15

                          Yes, and I'm saying that I've done it in C++, and I hate that I can't in C#.

                          Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

                          1 Reply Last reply
                          0
                          • E Eytukan

                            Do you think the "break;" [C++] command is really needed at the end of every case statement? I've never been in a situation where I'd want the control to pass on to the next case after satisfying a particular case. You know I missed to put a "break;" and it resulted in a big chaos :doh:.


                            Dario: How is "directory" in French? (I mean a file system directory). John Simmons: "zee file holdaire thingie"

                            B Offline
                            B Offline
                            Bradml
                            wrote on last edited by
                            #16

                            I like it, reminds the compiler that it isn't the only one who can break; things.


                            Brad Australian - Christian Graus on "Best books for VBscript" A big thick one, so you can whack yourself on the head with it.

                            1 Reply Last reply
                            0
                            • C Christian Graus

                              VuNic wrote:

                              I've never been in a situation where I'd want the control to pass on to the next case after satisfying a particular case.

                              I have, many times. I *hate* that .NET doesn't allow this, and forces me to put a break.

                              Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

                              R Offline
                              R Offline
                              Rohde
                              wrote on last edited by
                              #17

                              Well, you can still do a fall-through with empty cases: switch(value) { case 0: case 1: case 2: System.Console.Writeln("CodeProject.com"); break; }


                              "When you have made evil the means of survival, do not expect men to remain good. Do not expect them to stay moral and lose their lives for the purpose of becoming the fodder of the immoral. Do not expect them to produce, when production is punished and looting rewarded. Do not ask, `Who is destroying the world?' You are."
                              -Atlas Shrugged, Ayn Rand

                              C 1 Reply Last reply
                              0
                              • R Rohde

                                Well, you can still do a fall-through with empty cases: switch(value) { case 0: case 1: case 2: System.Console.Writeln("CodeProject.com"); break; }


                                "When you have made evil the means of survival, do not expect men to remain good. Do not expect them to stay moral and lose their lives for the purpose of becoming the fodder of the immoral. Do not expect them to produce, when production is punished and looting rewarded. Do not ask, `Who is destroying the world?' You are."
                                -Atlas Shrugged, Ayn Rand

                                C Offline
                                C Offline
                                Christian Graus
                                wrote on last edited by
                                #18

                                I know, but that's not enough.

                                Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

                                D 1 Reply Last reply
                                0
                                • E Eytukan

                                  Do you think the "break;" [C++] command is really needed at the end of every case statement? I've never been in a situation where I'd want the control to pass on to the next case after satisfying a particular case. You know I missed to put a "break;" and it resulted in a big chaos :doh:.


                                  Dario: How is "directory" in French? (I mean a file system directory). John Simmons: "zee file holdaire thingie"

                                  V Offline
                                  V Offline
                                  Vikram A Punathambekar
                                  wrote on last edited by
                                  #19

                                  VuNic wrote:

                                  Do you think the "break;" [C++] command is really needed at the end of every case statement?

                                  No. I think the default behavior is wrong here.

                                  VuNic wrote:

                                  I've never been in a situation where I'd want the control to pass on to the next case after satisfying a particular case.

                                  I have. [edit]I make sure it's commented prominently.[/edit]

                                  Cheers, Vikram.


                                  "...we are disempowered to cultivate in their communities an inclination to assimilate to our culture." - Stan Shannon.

                                  E 1 Reply Last reply
                                  0
                                  • C Christian Graus

                                    I know, but that's not enough.

                                    Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

                                    D Offline
                                    D Offline
                                    Daniel Turini
                                    wrote on last edited by
                                    #20

                                    You can also "goto" an specific case, which is probably what the compiler does after rearranging "case"s for performance.

                                    C 1 Reply Last reply
                                    0
                                    • realJSOPR realJSOP

                                      VuNic wrote:

                                      Do you think the "break;" [C++] command is really needed at the end of every case statement?

                                      No, it's not needed at the end of every one. There are rare instances where you might want to process code in one case but fall through to the next.

                                      VuNic wrote:

                                      I've never been in a situation where I'd want the control to pass on to the next case after satisfying a particular case.

                                      Then in your case, you need a break between cases.

                                      VuNic wrote:

                                      You know I missed to put a "break;" and it resulted in a big chaos

                                      Silver lining - you've become a better programmer as a result of the experience.

                                      "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                                      -----
                                      "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

                                      E Offline
                                      E Offline
                                      Eytukan
                                      wrote on last edited by
                                      #21

                                      John Simmons / outlaw programmer wrote:

                                      you've become a better programmer as a result of the experience.

                                      :-O:-O Thanks. It's encourging. Now I'm raring to make big mistakes :-O. [My boss::wtf:]


                                      Dario: How is "directory" in French? (I mean a file system directory). John Simmons: "zee file holdaire thingie"

                                      realJSOPR 1 Reply Last reply
                                      0
                                      • D Daniel Turini

                                        You can also "goto" an specific case, which is probably what the compiler does after rearranging "case"s for performance.

                                        C Offline
                                        C Offline
                                        Christian Graus
                                        wrote on last edited by
                                        #22

                                        Yep, they enforced break because otherwise the code is hard to read, and then they encouraged goto. Brilliant.

                                        Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

                                        J E 2 Replies Last reply
                                        0
                                        • E Eytukan

                                          Do you think the "break;" [C++] command is really needed at the end of every case statement? I've never been in a situation where I'd want the control to pass on to the next case after satisfying a particular case. You know I missed to put a "break;" and it resulted in a big chaos :doh:.


                                          Dario: How is "directory" in French? (I mean a file system directory). John Simmons: "zee file holdaire thingie"

                                          R Offline
                                          R Offline
                                          Rage
                                          wrote on last edited by
                                          #23

                                          VuNic wrote:

                                          where I'd want the control to pass on to the next case after satisfying a particular case

                                          In my area (micrcontroller) where most of the logic is done using state machine, switch-case are very popular, and we have very often the case that we fall through a case statement to another.

                                          Company policy : no access to the internet but CP ~RaGE()

                                          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