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. break through 2 loops

break through 2 loops

Scheduled Pinned Locked Moved C#
15 Posts 8 Posters 0 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 Offline
    D Offline
    Deresen
    wrote on last edited by
    #1

    Hey everyone, I would like to break through 2 loops with just one rule. Something like this:

    for(;;)//loop 1
    {
    for(;;)//loop 2
    {
    //do something to exit loop 1
    }
    }

    L L H 3 Replies Last reply
    0
    • D Deresen

      Hey everyone, I would like to break through 2 loops with just one rule. Something like this:

      for(;;)//loop 1
      {
      for(;;)//loop 2
      {
      //do something to exit loop 1
      }
      }

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

      Hi, there are several ways of breaking out of several loops: 1. change the state such that all the continuation tests will fail, and break (one way of achieving this is by adding a boolean test to each and every continuation test) 2. create a method that contains nothing but the loops, and return where you want to break out; 3. throw an exception and catch it where you need to. :)

      Luc Pattyn [Forum Guidelines] [My Articles]


      Love, happiness and fewer bugs for 2009!


      D R C 3 Replies Last reply
      0
      • D Deresen

        Hey everyone, I would like to break through 2 loops with just one rule. Something like this:

        for(;;)//loop 1
        {
        for(;;)//loop 2
        {
        //do something to exit loop 1
        }
        }

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

        You can use break for the second loop and a boolean variable to be used inside the condition for loop 1 and set this to true from loop 2. regards

        1 Reply Last reply
        0
        • L Luc Pattyn

          Hi, there are several ways of breaking out of several loops: 1. change the state such that all the continuation tests will fail, and break (one way of achieving this is by adding a boolean test to each and every continuation test) 2. create a method that contains nothing but the loops, and return where you want to break out; 3. throw an exception and catch it where you need to. :)

          Luc Pattyn [Forum Guidelines] [My Articles]


          Love, happiness and fewer bugs for 2009!


          D Offline
          D Offline
          Deresen
          wrote on last edited by
          #4

          Yes I've thought of those options too, but I don't want to break with extra code, not even a boolean. With 'break;' you will exit one loop. But isn't there something like break * 2;

          N L 2 Replies Last reply
          0
          • D Deresen

            Yes I've thought of those options too, but I don't want to break with extra code, not even a boolean. With 'break;' you will exit one loop. But isn't there something like break * 2;

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

            Deresen wrote:

            But isn't there something like break * 2;

            You will end up in goto.

            Navaneeth How to use google | Ask smart questions

            1 Reply Last reply
            0
            • D Deresen

              Yes I've thought of those options too, but I don't want to break with extra code, not even a boolean. With 'break;' you will exit one loop. But isn't there something like break * 2;

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

              Hi, No. Although there are languages that support break <count> C# isn't one of them. :)

              Luc Pattyn [Forum Guidelines] [My Articles]


              Love, happiness and fewer bugs for 2009!


              1 Reply Last reply
              0
              • L Luc Pattyn

                Hi, there are several ways of breaking out of several loops: 1. change the state such that all the continuation tests will fail, and break (one way of achieving this is by adding a boolean test to each and every continuation test) 2. create a method that contains nothing but the loops, and return where you want to break out; 3. throw an exception and catch it where you need to. :)

                Luc Pattyn [Forum Guidelines] [My Articles]


                Love, happiness and fewer bugs for 2009!


                R Offline
                R Offline
                Rob Philpott
                wrote on last edited by
                #7

                Luc Pattyn wrote:

                3. throw an exception and catch it where you need to.

                Exceptions to control program flow? Isn't that a crime?

                Regards, Rob Philpott.

                L C 2 Replies Last reply
                0
                • R Rob Philpott

                  Luc Pattyn wrote:

                  3. throw an exception and catch it where you need to.

                  Exceptions to control program flow? Isn't that a crime?

                  Regards, Rob Philpott.

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

                  Hi,

                  Rob Philpott wrote:

                  Exceptions to control program flow? Isn't that a crime?

                  Not always. The OP didn't mention why nor how often he would break out of all loops. Exceptions always operate on program flow, they exist to make life easier and code more reliable. Not making good use of available means would be a crime. Depending on circumstances, one has to make a good choice, that is why I gave three possibilities. :)

                  Luc Pattyn [Forum Guidelines] [My Articles]


                  Love, happiness and fewer bugs for 2009!


                  1 Reply Last reply
                  0
                  • L Luc Pattyn

                    Hi, there are several ways of breaking out of several loops: 1. change the state such that all the continuation tests will fail, and break (one way of achieving this is by adding a boolean test to each and every continuation test) 2. create a method that contains nothing but the loops, and return where you want to break out; 3. throw an exception and catch it where you need to. :)

                    Luc Pattyn [Forum Guidelines] [My Articles]


                    Love, happiness and fewer bugs for 2009!


                    C Offline
                    C Offline
                    CPallini
                    wrote on last edited by
                    #9

                    4.use goto. :rolleyes: :)

                    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                    This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                    [My articles]

                    L 1 Reply Last reply
                    0
                    • R Rob Philpott

                      Luc Pattyn wrote:

                      3. throw an exception and catch it where you need to.

                      Exceptions to control program flow? Isn't that a crime?

                      Regards, Rob Philpott.

                      C Offline
                      C Offline
                      CPallini
                      wrote on last edited by
                      #10

                      Rob Philpott wrote:

                      Exceptions to control program flow? Isn't that a crime?

                      Nope, if the condition that breaks the loops is exceptional. :-D

                      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                      [My articles]

                      R 1 Reply Last reply
                      0
                      • C CPallini

                        4.use goto. :rolleyes: :)

                        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                        [My articles]

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

                        Absolutely. That is covered by "Not making good use of available means would be a crime". :-D

                        Luc Pattyn [Forum Guidelines] [My Articles]


                        Love, happiness and fewer bugs for 2009!


                        D 1 Reply Last reply
                        0
                        • C CPallini

                          Rob Philpott wrote:

                          Exceptions to control program flow? Isn't that a crime?

                          Nope, if the condition that breaks the loops is exceptional. :-D

                          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                          [My articles]

                          R Offline
                          R Offline
                          Rob Philpott
                          wrote on last edited by
                          #12

                          Agreed. But use otherwise deserves a slap!

                          Regards, Rob Philpott.

                          1 Reply Last reply
                          0
                          • L Luc Pattyn

                            Absolutely. That is covered by "Not making good use of available means would be a crime". :-D

                            Luc Pattyn [Forum Guidelines] [My Articles]


                            Love, happiness and fewer bugs for 2009!


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

                            Luc Pattyn wrote:

                            Absolutely

                            I agree, in fact this is the perfect example of when a goto may be the most appropriate. It can be a useful statement - it's only because it can be (and has been) wildly misused that it's frowned upon. :-D

                            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)

                            D 1 Reply Last reply
                            0
                            • D Deresen

                              Hey everyone, I would like to break through 2 loops with just one rule. Something like this:

                              for(;;)//loop 1
                              {
                              for(;;)//loop 2
                              {
                              //do something to exit loop 1
                              }
                              }

                              H Offline
                              H Offline
                              HosamAly
                              wrote on last edited by
                              #14

                              If you were using Java, I'd say "break OuterLoop;" (by adding the label "OuterLoop:" before the outer loop). But since you're in C#, you need to do this:

                              for ( ; ; )
                              {
                              for ( ; ; )
                              {
                              goto EndOfLoop;
                              }
                              }
                              EndOfLoop:
                              // your code

                              (You may also check the second example in this[^] MSDN reference page, although I wouldn't like to see it in actual code!)

                              My LinkedIn Profile

                              1 Reply Last reply
                              0
                              • D DaveyM69

                                Luc Pattyn wrote:

                                Absolutely

                                I agree, in fact this is the perfect example of when a goto may be the most appropriate. It can be a useful statement - it's only because it can be (and has been) wildly misused that it's frowned upon. :-D

                                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)

                                D Offline
                                D Offline
                                Deresen
                                wrote on last edited by
                                #15

                                I love the goto statement, so I will use it. It let's me think of the good times programming microcontrollers in assembly!!!

                                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