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. Loop exit

Loop exit

Scheduled Pinned Locked Moved The Lounge
csharpc++javadelphialgorithms
53 Posts 24 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.
  • S Stefan_Lang

    I haven't read all the responses that may or may not give a hint in that direction, but what exactly is it that these commands do that a break statement in C doesn't?

    GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)

    H Offline
    H Offline
    harvyk0
    wrote on last edited by
    #29

    Here is a real world example for you

    for (property = 0, len = obj.length; property < len; property++) {
    if (callback.call(obj[property], property, obj[property]) === false) {
    break;
    }
    }

    Once the following is true (callback.call(obj[property], property, obj[property]) === false), there is no point in continuing the loop, as a result the break will exit the loop. If the method has the answer it is looking for, you can also do

    for (property = 0, len = obj.length; property < len; property++) {
    if (callback.call(obj[property], property, obj[property]) === false) {
    return 1;
    }
    }

    so that not only will the loop end, but if there is nothing more in the method which will add value to the answer, the data is returned without needing to continue (bad choice of words, since continue has it's own special meaning) the loop and without needing to look at any more code.

    S 1 Reply Last reply
    0
    • H harvyk0

      Here is a real world example for you

      for (property = 0, len = obj.length; property < len; property++) {
      if (callback.call(obj[property], property, obj[property]) === false) {
      break;
      }
      }

      Once the following is true (callback.call(obj[property], property, obj[property]) === false), there is no point in continuing the loop, as a result the break will exit the loop. If the method has the answer it is looking for, you can also do

      for (property = 0, len = obj.length; property < len; property++) {
      if (callback.call(obj[property], property, obj[property]) === false) {
      return 1;
      }
      }

      so that not only will the loop end, but if there is nothing more in the method which will add value to the answer, the data is returned without needing to continue (bad choice of words, since continue has it's own special meaning) the loop and without needing to look at any more code.

      S Offline
      S Offline
      Stefan_Lang
      wrote on last edited by
      #30

      That wasn't my question at all. I know what break does. And exactly because I understand what it does, I do not understand the original question! I don't know Planc, but from the original posting my understanding was that the commands pointed out there - exitfor, exitwhile - simply exit from the loop. Just like break does. I don't see the difference, and therefore I don't see the point of the question.

      GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)

      L 1 Reply Last reply
      0
      • M Mark_Wallace

        Shirley, using goto is simpler.

        I wanna be a eunuchs developer! Pass me a bread knife!

        S Offline
        S Offline
        Simon ORiordan from UK
        wrote on last edited by
        #31

        Goto the cockpit and see what the hold up is. And don't call me Shirley.

        1 Reply Last reply
        0
        • R Ravi Bhavnani

          Why?  Using a goto to exit a loop is one its few (perhaps only) valid use cases. /ravi

          My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com

          S Offline
          S Offline
          SortaCore
          wrote on last edited by
          #32

          I've been known to use it for "load variable amounts of stuff from DB as needed" and use goto to get to the cleanup/UI enabling at the end. It probably comes from the habit of preferring:

          void someFunc()
          {
          if (!A)
          return;
          DoStuffWithA()
          DoMoreCrud();
          }

          rather than

          void someFunc()
          {
          if (A)
          {
          DoStuffWithA();
          DoMoreCrud();
          }
          }
          // TWO close braces with no code between? Surely you jest.

          1 Reply Last reply
          0
          • S Stefan_Lang

            That wasn't my question at all. I know what break does. And exactly because I understand what it does, I do not understand the original question! I don't know Planc, but from the original posting my understanding was that the commands pointed out there - exitfor, exitwhile - simply exit from the loop. Just like break does. I don't see the difference, and therefore I don't see the point of the question.

            GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)

            L Offline
            L Offline
            L Braun
            wrote on last edited by
            #33

            After a loop, how do you know if you finished it or breaked out of it? This post is not about just leaving a loop, but about knowing how you left it and act according it. We probably all know how to do that in c, but this is about a language that adds syntax elements for that.

            M S 2 Replies Last reply
            0
            • L L Braun

              After a loop, how do you know if you finished it or breaked out of it? This post is not about just leaving a loop, but about knowing how you left it and act according it. We probably all know how to do that in c, but this is about a language that adds syntax elements for that.

              M Offline
              M Offline
              Mark_Wallace
              wrote on last edited by
              #34

              Looks like a nonsense reason, to me. You can just put a message before the break statement.

              I wanna be a eunuchs developer! Pass me a bread knife!

              M 1 Reply Last reply
              0
              • M Mark_Wallace

                Looks like a nonsense reason, to me. You can just put a message before the break statement.

                I wanna be a eunuchs developer! Pass me a bread knife!

                M Offline
                M Offline
                Mark_Wallace
                wrote on last edited by
                #35

                Mark_Wallace wrote:

                You can just put a message before the break statement.

                Goto! Before the goto statement! Damn!

                I wanna be a eunuchs developer! Pass me a bread knife!

                1 Reply Last reply
                0
                • L L Braun

                  After a loop, how do you know if you finished it or breaked out of it? This post is not about just leaving a loop, but about knowing how you left it and act according it. We probably all know how to do that in c, but this is about a language that adds syntax elements for that.

                  S Offline
                  S Offline
                  Stefan_Lang
                  wrote on last edited by
                  #36

                  How do you know the difference in Planc if you used exit***? As I said, I don't understand what, exactly, these statements do, and the OP doesn't inidicate they do anything beyond breaking out of the loop. That's what break does, too. Hence my question.

                  GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)

                  L 1 Reply Last reply
                  0
                  • K kalberts

                    Running into a feature-by-feature language comparison made me think back of a feature I saw in one single langugage, but would fit very nicely into a lot of the pascal/c/java/... class of languages: Alternate loop exits. When iterating through a list, an array or some sort of collection, objects are not all treated equally: You reach a sentinel, find the object you're searching for, reach the capacity of the bucket you are filling up, or whatever. The job has successfully been done, so you exit the loop. Or, you do not complete the job: There is no sentinel (because the buffer is completely filled), the desired object is not found, or the bucket has still some capacitly left. Running through the collection to the end or not running to the end are different situations, frequently requiring different handling. In most languages, an early exit requires that you set some boolean flag decleared outside the loop, then break (or whatever the keyword is in your favorite language), and after the loop you add an if-statement, syntactically detached from the loop, to provide differnt treatment based on the setting of the flag. I was programming in this language called Planc - "Programming LANguage for Nord Computers", a vendor specific systems implementation - remmebered by noone today. It had this nice syntactic sugar:

                    for listpointer in listhead:nextfield do
                    
                      ... processing list element as desired
                    
                      while listpointer.keyvalue <> desidred\_key
                    
                      ... porcessing list element as desired
                    
                    exitwhile
                      ... the desired list element was found, 
                      write("list element was found and processed")
                    
                    exitfor 
                      ... reached end of list without finding the desired element
                      write("no element with the desired key was found in the list")
                    
                    endfor
                    

                    No need for any one-time-use bool cluttering up variable space. No need to introduce a separate block for testing and breaking out. No need for a detached if-statement - the different loop exit handling is syntactically integrated with the loop itself. I never saw this sort of construct in any other language, but I have been missing it hundred of times. Are there other languages out there with something similar? Certainly not C, C++, Java, C#, Pascal, ... And, by the way: The above specification of the iteration is a nice syntactic sugar for what would be in C-like languages:

                    for (listptrtype listpointer = listhead; listpointer != null; listpointer =

                    E Offline
                    E Offline
                    Eduard Matei
                    wrote on last edited by
                    #37

                    "Are there other languages out there with something similar?" Python has this "syntactic sugar" for alternate loop exit:

                    for item in iterable:
                    if condition(item):
                    break
                    process(item)
                    else:
                    print("No item in iterable meets the condition")

                    The else signifies that the for loop has finished without "break"-ing.

                    1 Reply Last reply
                    0
                    • K kalberts

                      Running into a feature-by-feature language comparison made me think back of a feature I saw in one single langugage, but would fit very nicely into a lot of the pascal/c/java/... class of languages: Alternate loop exits. When iterating through a list, an array or some sort of collection, objects are not all treated equally: You reach a sentinel, find the object you're searching for, reach the capacity of the bucket you are filling up, or whatever. The job has successfully been done, so you exit the loop. Or, you do not complete the job: There is no sentinel (because the buffer is completely filled), the desired object is not found, or the bucket has still some capacitly left. Running through the collection to the end or not running to the end are different situations, frequently requiring different handling. In most languages, an early exit requires that you set some boolean flag decleared outside the loop, then break (or whatever the keyword is in your favorite language), and after the loop you add an if-statement, syntactically detached from the loop, to provide differnt treatment based on the setting of the flag. I was programming in this language called Planc - "Programming LANguage for Nord Computers", a vendor specific systems implementation - remmebered by noone today. It had this nice syntactic sugar:

                      for listpointer in listhead:nextfield do
                      
                        ... processing list element as desired
                      
                        while listpointer.keyvalue <> desidred\_key
                      
                        ... porcessing list element as desired
                      
                      exitwhile
                        ... the desired list element was found, 
                        write("list element was found and processed")
                      
                      exitfor 
                        ... reached end of list without finding the desired element
                        write("no element with the desired key was found in the list")
                      
                      endfor
                      

                      No need for any one-time-use bool cluttering up variable space. No need to introduce a separate block for testing and breaking out. No need for a detached if-statement - the different loop exit handling is syntactically integrated with the loop itself. I never saw this sort of construct in any other language, but I have been missing it hundred of times. Are there other languages out there with something similar? Certainly not C, C++, Java, C#, Pascal, ... And, by the way: The above specification of the iteration is a nice syntactic sugar for what would be in C-like languages:

                      for (listptrtype listpointer = listhead; listpointer != null; listpointer =

                      M Offline
                      M Offline
                      Matthew Barnett
                      wrote on last edited by
                      #38

                      In Python, loops can take an 'else' clause. It's run if you don't break out of the loop. For example:

                      for item in collection:
                      if some_test(item):
                      print('Found one!')
                      break
                      else:
                      print('No match found.')

                      K 1 Reply Last reply
                      0
                      • S Stefan_Lang

                        How do you know the difference in Planc if you used exit***? As I said, I don't understand what, exactly, these statements do, and the OP doesn't inidicate they do anything beyond breaking out of the loop. That's what break does, too. Hence my question.

                        GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)

                        L Offline
                        L Offline
                        L Braun
                        wrote on last edited by
                        #39

                        After reading the original post again, I wasn't sure I got it right, so I had a look in wikipedia about PLANC. Now I think EXITFOR specifies what to do if the for loop exits normally and the EXITWHILE specifies what to do when a WHILE clause (one of many) becomes true. Both blocks are specified inside the loop. So I see a WHILE in PLANC as a "if() break;" construction in c. And EXITFOR and EXITWHILE would be coded as something like if (got_out_with_break) {// EXITWHILE block} else {// EXITFOR block} but I could be wrong :) As stated below - Python got it as well.

                        S 1 Reply Last reply
                        0
                        • K kalberts

                          Running into a feature-by-feature language comparison made me think back of a feature I saw in one single langugage, but would fit very nicely into a lot of the pascal/c/java/... class of languages: Alternate loop exits. When iterating through a list, an array or some sort of collection, objects are not all treated equally: You reach a sentinel, find the object you're searching for, reach the capacity of the bucket you are filling up, or whatever. The job has successfully been done, so you exit the loop. Or, you do not complete the job: There is no sentinel (because the buffer is completely filled), the desired object is not found, or the bucket has still some capacitly left. Running through the collection to the end or not running to the end are different situations, frequently requiring different handling. In most languages, an early exit requires that you set some boolean flag decleared outside the loop, then break (or whatever the keyword is in your favorite language), and after the loop you add an if-statement, syntactically detached from the loop, to provide differnt treatment based on the setting of the flag. I was programming in this language called Planc - "Programming LANguage for Nord Computers", a vendor specific systems implementation - remmebered by noone today. It had this nice syntactic sugar:

                          for listpointer in listhead:nextfield do
                          
                            ... processing list element as desired
                          
                            while listpointer.keyvalue <> desidred\_key
                          
                            ... porcessing list element as desired
                          
                          exitwhile
                            ... the desired list element was found, 
                            write("list element was found and processed")
                          
                          exitfor 
                            ... reached end of list without finding the desired element
                            write("no element with the desired key was found in the list")
                          
                          endfor
                          

                          No need for any one-time-use bool cluttering up variable space. No need to introduce a separate block for testing and breaking out. No need for a detached if-statement - the different loop exit handling is syntactically integrated with the loop itself. I never saw this sort of construct in any other language, but I have been missing it hundred of times. Are there other languages out there with something similar? Certainly not C, C++, Java, C#, Pascal, ... And, by the way: The above specification of the iteration is a nice syntactic sugar for what would be in C-like languages:

                          for (listptrtype listpointer = listhead; listpointer != null; listpointer =

                          M Offline
                          M Offline
                          Mike E Andrews
                          wrote on last edited by
                          #40

                          Visual Basic.NET supports similar constructs, such as:

                          Exit For
                          Exit While
                          Exit Do

                          and continuations like:

                          Continue For
                          Continue While
                          Continue Do

                          C 1 Reply Last reply
                          0
                          • L L Braun

                            After reading the original post again, I wasn't sure I got it right, so I had a look in wikipedia about PLANC. Now I think EXITFOR specifies what to do if the for loop exits normally and the EXITWHILE specifies what to do when a WHILE clause (one of many) becomes true. Both blocks are specified inside the loop. So I see a WHILE in PLANC as a "if() break;" construction in c. And EXITFOR and EXITWHILE would be coded as something like if (got_out_with_break) {// EXITWHILE block} else {// EXITFOR block} but I could be wrong :) As stated below - Python got it as well.

                            S Offline
                            S Offline
                            Stefan_Lang
                            wrote on last edited by
                            #41

                            After reading the python remark below I think I've got it (good thing you pointed that out! :thumbsup:) I'm not quite convinced of the benefits though. It may indeed - as the OP stated - safe you an extra if or flag variable. But the price you pay is readability: the conditional code can be in an entirely different place than the condition, with potentially a lot of code in between. Even worse, after reading over the OP again, it seems like there can be several while statements that can all trigger the same exitwhile, meaning that there can be several conditons that are all in different places, separated from the conditional code and the other conditions as well! How on earth are you supposed to keep track of the flow of control in code like that? :confused: There may be cases where such a language construct may make sense, and even be better readable than the alternatives offered in C and other languages. But I sense a great potential of abuse, and I suspect it takes both experience and sense of responsibility to use it well. Should a programming language support such a feature? If you say "yes", will you also agree that cars should be allowed to use the sidewalks (provided they are wide enough)?. These are the same questions! So, the answer is also the same: we cannot assume that people will use that option responsibly, so we're better off without it!

                            GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)

                            K 1 Reply Last reply
                            0
                            • K kalberts

                              Running into a feature-by-feature language comparison made me think back of a feature I saw in one single langugage, but would fit very nicely into a lot of the pascal/c/java/... class of languages: Alternate loop exits. When iterating through a list, an array or some sort of collection, objects are not all treated equally: You reach a sentinel, find the object you're searching for, reach the capacity of the bucket you are filling up, or whatever. The job has successfully been done, so you exit the loop. Or, you do not complete the job: There is no sentinel (because the buffer is completely filled), the desired object is not found, or the bucket has still some capacitly left. Running through the collection to the end or not running to the end are different situations, frequently requiring different handling. In most languages, an early exit requires that you set some boolean flag decleared outside the loop, then break (or whatever the keyword is in your favorite language), and after the loop you add an if-statement, syntactically detached from the loop, to provide differnt treatment based on the setting of the flag. I was programming in this language called Planc - "Programming LANguage for Nord Computers", a vendor specific systems implementation - remmebered by noone today. It had this nice syntactic sugar:

                              for listpointer in listhead:nextfield do
                              
                                ... processing list element as desired
                              
                                while listpointer.keyvalue <> desidred\_key
                              
                                ... porcessing list element as desired
                              
                              exitwhile
                                ... the desired list element was found, 
                                write("list element was found and processed")
                              
                              exitfor 
                                ... reached end of list without finding the desired element
                                write("no element with the desired key was found in the list")
                              
                              endfor
                              

                              No need for any one-time-use bool cluttering up variable space. No need to introduce a separate block for testing and breaking out. No need for a detached if-statement - the different loop exit handling is syntactically integrated with the loop itself. I never saw this sort of construct in any other language, but I have been missing it hundred of times. Are there other languages out there with something similar? Certainly not C, C++, Java, C#, Pascal, ... And, by the way: The above specification of the iteration is a nice syntactic sugar for what would be in C-like languages:

                              for (listptrtype listpointer = listhead; listpointer != null; listpointer =

                              M Offline
                              M Offline
                              Member 4608898
                              wrote on last edited by
                              #42

                              But if you had for (...) { for (...) { I want to exitfor from the outer loop from a condition in the inner loop } } How would you do it? goto was invented for a reason and this is it!!! :-D

                              K 1 Reply Last reply
                              0
                              • M Mike E Andrews

                                Visual Basic.NET supports similar constructs, such as:

                                Exit For
                                Exit While
                                Exit Do

                                and continuations like:

                                Continue For
                                Continue While
                                Continue Do

                                C Offline
                                C Offline
                                crazedDotNetDev
                                wrote on last edited by
                                #43

                                In fact I seem to remember an "exit for" in QBASIC. Loop exits go way back in many BASIC compliers. But all you C# coders don't worry; you're still "the best". You'll become better coders as C# becomes more like BASIC. :wtf: LOL, sorry couldn't help myself. :-O

                                - great coders make code look easy - When humans are doing things computers could be doing instead, the computers get together late at night and laugh at us. - ¿Neal Ford?

                                K 1 Reply Last reply
                                0
                                • K kalberts

                                  Running into a feature-by-feature language comparison made me think back of a feature I saw in one single langugage, but would fit very nicely into a lot of the pascal/c/java/... class of languages: Alternate loop exits. When iterating through a list, an array or some sort of collection, objects are not all treated equally: You reach a sentinel, find the object you're searching for, reach the capacity of the bucket you are filling up, or whatever. The job has successfully been done, so you exit the loop. Or, you do not complete the job: There is no sentinel (because the buffer is completely filled), the desired object is not found, or the bucket has still some capacitly left. Running through the collection to the end or not running to the end are different situations, frequently requiring different handling. In most languages, an early exit requires that you set some boolean flag decleared outside the loop, then break (or whatever the keyword is in your favorite language), and after the loop you add an if-statement, syntactically detached from the loop, to provide differnt treatment based on the setting of the flag. I was programming in this language called Planc - "Programming LANguage for Nord Computers", a vendor specific systems implementation - remmebered by noone today. It had this nice syntactic sugar:

                                  for listpointer in listhead:nextfield do
                                  
                                    ... processing list element as desired
                                  
                                    while listpointer.keyvalue <> desidred\_key
                                  
                                    ... porcessing list element as desired
                                  
                                  exitwhile
                                    ... the desired list element was found, 
                                    write("list element was found and processed")
                                  
                                  exitfor 
                                    ... reached end of list without finding the desired element
                                    write("no element with the desired key was found in the list")
                                  
                                  endfor
                                  

                                  No need for any one-time-use bool cluttering up variable space. No need to introduce a separate block for testing and breaking out. No need for a detached if-statement - the different loop exit handling is syntactically integrated with the loop itself. I never saw this sort of construct in any other language, but I have been missing it hundred of times. Are there other languages out there with something similar? Certainly not C, C++, Java, C#, Pascal, ... And, by the way: The above specification of the iteration is a nice syntactic sugar for what would be in C-like languages:

                                  for (listptrtype listpointer = listhead; listpointer != null; listpointer =

                                  D Offline
                                  D Offline
                                  dg6yhw11
                                  wrote on last edited by
                                  #44

                                  BASIC provides several exit statements such as EXIT LOOP EXIT FOR You can also set the condition in the call to the loop WHILE Not [answer you want is found] ... WEND will exit when the condition is met (of course you have to add another exit test to avoid infinite loops :) There are a bunch of similar constructs. To use this with some OOP type language just write a little function in BASIC (Visual Studio, PowerBasic), compile to a DLL and call it from the object you need to use. Visual Studio would probably let you do it all in the same project but then you wouldn't have the DLL to reuse.

                                  1 Reply Last reply
                                  0
                                  • K kalberts

                                    Running into a feature-by-feature language comparison made me think back of a feature I saw in one single langugage, but would fit very nicely into a lot of the pascal/c/java/... class of languages: Alternate loop exits. When iterating through a list, an array or some sort of collection, objects are not all treated equally: You reach a sentinel, find the object you're searching for, reach the capacity of the bucket you are filling up, or whatever. The job has successfully been done, so you exit the loop. Or, you do not complete the job: There is no sentinel (because the buffer is completely filled), the desired object is not found, or the bucket has still some capacitly left. Running through the collection to the end or not running to the end are different situations, frequently requiring different handling. In most languages, an early exit requires that you set some boolean flag decleared outside the loop, then break (or whatever the keyword is in your favorite language), and after the loop you add an if-statement, syntactically detached from the loop, to provide differnt treatment based on the setting of the flag. I was programming in this language called Planc - "Programming LANguage for Nord Computers", a vendor specific systems implementation - remmebered by noone today. It had this nice syntactic sugar:

                                    for listpointer in listhead:nextfield do
                                    
                                      ... processing list element as desired
                                    
                                      while listpointer.keyvalue <> desidred\_key
                                    
                                      ... porcessing list element as desired
                                    
                                    exitwhile
                                      ... the desired list element was found, 
                                      write("list element was found and processed")
                                    
                                    exitfor 
                                      ... reached end of list without finding the desired element
                                      write("no element with the desired key was found in the list")
                                    
                                    endfor
                                    

                                    No need for any one-time-use bool cluttering up variable space. No need to introduce a separate block for testing and breaking out. No need for a detached if-statement - the different loop exit handling is syntactically integrated with the loop itself. I never saw this sort of construct in any other language, but I have been missing it hundred of times. Are there other languages out there with something similar? Certainly not C, C++, Java, C#, Pascal, ... And, by the way: The above specification of the iteration is a nice syntactic sugar for what would be in C-like languages:

                                    for (listptrtype listpointer = listhead; listpointer != null; listpointer =

                                    F Offline
                                    F Offline
                                    Frank Malcolm
                                    wrote on last edited by
                                    #45

                                    Delphi's version of Pascal has Break (quit the loop) and Continue (begin the next iteration).

                                    K 1 Reply Last reply
                                    0
                                    • F Frank Malcolm

                                      Delphi's version of Pascal has Break (quit the loop) and Continue (begin the next iteration).

                                      K Offline
                                      K Offline
                                      kalberts
                                      wrote on last edited by
                                      #46

                                      That is like break and continue in C, isn't it? Does it provide two alternatative ways out of a loop: One alternative is executed if the break was performed,the other if break was not performed? If it does, some more syntax must be defined.

                                      F 1 Reply Last reply
                                      0
                                      • M Member 4608898

                                        But if you had for (...) { for (...) { I want to exitfor from the outer loop from a condition in the inner loop } } How would you do it? goto was invented for a reason and this is it!!! :-D

                                        K Offline
                                        K Offline
                                        kalberts
                                        wrote on last edited by
                                        #47

                                        CHILL has a very nice solution to this: Every block, whether a procedure, loop, switch or even a linear sequence of statements enclosed with BEGIN and END, could be prefixed with a label. In CHILL, a label does not identify a point in the program, but a block, and consequently label scopes could be nested. So to leave the outer loop, you would write

                                        OuterLoop:
                                        DO FOR (...)

                                        InnerLoop:
                                        DO FOR (...)
                                        ...
                                        IF THEN EXIT InnerLoop; FI;
                                        ...
                                        IF THEN EXIT OuterLoop; FI
                                        ...
                                        OD

                                        OD

                                        (Here I illustrate both leaving the inner loop and the outer loop.) However, CHILL doesn't provide what I asked for in my original post: If the post-loop processing depends on whether you completed the loop or left prematurely by EXIT, you must set some variable to a magic value and test it after the loop, and the post-loop processing would syntactically (e.g. with respect to variable scope) be outside the loop. While we are at CHILL: Another nifty syntactic sugar cube is the keyword EVER:

                                        DO FOR EVER
                                        ...
                                        OD

                                        The semantics of EVER is quite obvious. I like this so much that whenever I need to program an inner loop in C, I set up a

                                        #define ever (;;)

                                        to be able to code it as "for ever {...}" in C. Sure, any well seasoned C programmer would prefer "while (1) {...}", but even though I have been writing more lines of C code than in any other language the last thirty years, I still read it as "while one what???"

                                        1 Reply Last reply
                                        0
                                        • C crazedDotNetDev

                                          In fact I seem to remember an "exit for" in QBASIC. Loop exits go way back in many BASIC compliers. But all you C# coders don't worry; you're still "the best". You'll become better coders as C# becomes more like BASIC. :wtf: LOL, sorry couldn't help myself. :-O

                                          - great coders make code look easy - When humans are doing things computers could be doing instead, the computers get together late at night and laugh at us. - ¿Neal Ford?

                                          K Offline
                                          K Offline
                                          kalberts
                                          wrote on last edited by
                                          #48

                                          But does it provide two ways out? I.e. alternate code blocks executed one but not the other, depending on how you exited the loop?

                                          C 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