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 / C++ / MFC
  4. Break in a for loop

Break in a for loop

Scheduled Pinned Locked Moved C / C++ / MFC
question
8 Posts 7 Posters 53 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.
  • C Offline
    C Offline
    Calin Negru
    wrote on last edited by
    #1

    Calling break inside nested for loops breaks all loops or just the inner loop where break is found? Do I need to do this to exit all loops?

    bool breakouter = false;
    for(int x=0;x

    Mircea NeacsuM M B 3 Replies Last reply
    0
    • C Calin Negru

      Calling break inside nested for loops breaks all loops or just the inner loop where break is found? Do I need to do this to exit all loops?

      bool breakouter = false;
      for(int x=0;x

      Mircea NeacsuM Offline
      Mircea NeacsuM Offline
      Mircea Neacsu
      wrote on last edited by
      #2

      Only the inner loop. If you want to break out of more loops you can do something like:

      for(int x=0;x
      Mircea

      1 Reply Last reply
      0
      • C Calin Negru

        Calling break inside nested for loops breaks all loops or just the inner loop where break is found? Do I need to do this to exit all loops?

        bool breakouter = false;
        for(int x=0;x

        M Offline
        M Offline
        Maximilien
        wrote on last edited by
        #3

        Probably one of the last valid usecase for goto. Especially if more than 2 inner loops. Obviously, if your loops are doing many resources allocations, you'll need to do manual cleaning.

        CI/CD = Continuous Impediment/Continuous Despair

        C CPalliniC T 3 Replies Last reply
        0
        • M Maximilien

          Probably one of the last valid usecase for goto. Especially if more than 2 inner loops. Obviously, if your loops are doing many resources allocations, you'll need to do manual cleaning.

          CI/CD = Continuous Impediment/Continuous Despair

          C Offline
          C Offline
          Calin Negru
          wrote on last edited by
          #4

          Thank you guys

          1 Reply Last reply
          0
          • M Maximilien

            Probably one of the last valid usecase for goto. Especially if more than 2 inner loops. Obviously, if your loops are doing many resources allocations, you'll need to do manual cleaning.

            CI/CD = Continuous Impediment/Continuous Despair

            CPalliniC Offline
            CPalliniC Offline
            CPallini
            wrote on last edited by
            #5

            Nah, using C++, that's horribile visu, but we could make an exception for you... :laugh:

            "In testa che avete, Signor di Ceprano?" -- Rigoletto

            In testa che avete, signor di Ceprano?

            1 Reply Last reply
            0
            • M Maximilien

              Probably one of the last valid usecase for goto. Especially if more than 2 inner loops. Obviously, if your loops are doing many resources allocations, you'll need to do manual cleaning.

              CI/CD = Continuous Impediment/Continuous Despair

              T Offline
              T Offline
              trønderen
              wrote on last edited by
              #6

              Maximilien wrote:

              Probably one of the last valid usecase for goto. Especially if more than 2 inner loops.

              There were lots of valid 'goto' use cases cases, before for loops, while loops, and arbitrarily nested if-else, were invented. Structured exit from multiply nested loops were invented 40+ years ago. In 1980 vintage CHILL, you could do

              outermost: intermediate: inner: exit intermediate;

              exit intermediate; would skip the "more intermediate statements" as well as further intermediate iterations, going directly to "more outermost statements" and continue with further outermost iterations. If it rather said exit outermost;, the "more outermost statements" would be skipped as well, and the outermost loop would be left. exit would be a short form of exit innermost; This construct never made it into mainstream language such as the C derived ones. One partial explanation is differences in label semantics: In CHILL, a label identifies a block, not a point in the code. Furthermore, a statement is a block; it need not be embraced to be labeled. I would be willing to trade a lot of other 'improvements' in more recent language extensions in favor of something like this. Some concepts were not well developed in 1980. If a language of today were to introduce such a multi-level exit, it would probably come with something 'finally'-like mechanisms to do required cleanup operations regardless of loop exit method.

              Religious freedom is the freedom to say that two plus two make five.

              Greg UtasG 1 Reply Last reply
              0
              • T trønderen

                Maximilien wrote:

                Probably one of the last valid usecase for goto. Especially if more than 2 inner loops.

                There were lots of valid 'goto' use cases cases, before for loops, while loops, and arbitrarily nested if-else, were invented. Structured exit from multiply nested loops were invented 40+ years ago. In 1980 vintage CHILL, you could do

                outermost: intermediate: inner: exit intermediate;

                exit intermediate; would skip the "more intermediate statements" as well as further intermediate iterations, going directly to "more outermost statements" and continue with further outermost iterations. If it rather said exit outermost;, the "more outermost statements" would be skipped as well, and the outermost loop would be left. exit would be a short form of exit innermost; This construct never made it into mainstream language such as the C derived ones. One partial explanation is differences in label semantics: In CHILL, a label identifies a block, not a point in the code. Furthermore, a statement is a block; it need not be embraced to be labeled. I would be willing to trade a lot of other 'improvements' in more recent language extensions in favor of something like this. Some concepts were not well developed in 1980. If a language of today were to introduce such a multi-level exit, it would probably come with something 'finally'-like mechanisms to do required cleanup operations regardless of loop exit method.

                Religious freedom is the freedom to say that two plus two make five.

                Greg UtasG Offline
                Greg UtasG Offline
                Greg Utas
                wrote on last edited by
                #7

                The proprietary language in which I worked for many years had exactly what you describe. A label was for a BLOCK/ENDBLOCK that you could EXIT. A label could also precede IF, DO, and the equivalents of switch) without using the BLOCK keyword. The language was designed in the late 70s. This was in the telecom sector, so they probably took the design from CHILL.

                Robust Services Core | Software Techniques for Lemmings | Articles
                The fox knows many things, but the hedgehog knows one big thing.

                <p><a href="https://github.com/GregUtas/robust-services-core/blob/master/README.md">Robust Services Core</a>
                <em>The fox knows many things, but the hedgehog knows one big thing.</em></p>

                1 Reply Last reply
                0
                • C Calin Negru

                  Calling break inside nested for loops breaks all loops or just the inner loop where break is found? Do I need to do this to exit all loops?

                  bool breakouter = false;
                  for(int x=0;x

                  B Offline
                  B Offline
                  BernardIE5317
                  wrote on last edited by
                  #8

                  Whenever I have such questions and I have them often I experiment w/ brief simple code and observe the behavior.

                  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