Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Control flow
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Loops == A loop is a sequence of statements which is specified once but which may be carried out several times in succession. The code "inside" the loop (the ''body'' of the loop, shown below as ''xxx'') is obeyed a specified number of times, or once for each of a collection of items, or until some condition is met, or [[Infinite loop|indefinitely]]. When one of those items is itself also a loop, it is called a "nested loop".<ref>{{Cite web |date=2019-11-25 |title=Nested Loops in C with Examples |url=https://www.geeksforgeeks.org/nested-loops-in-c-with-examples/ |access-date=2024-03-14 |website=GeeksforGeeks |language=en-US}}</ref><ref>{{Cite web |title=Python Nested Loops |url=https://www.w3schools.com/python/gloss_python_for_nested.asp |access-date=2024-03-14 |website=www.w3schools.com |language=en-US}}</ref><ref>{{Cite web |last=Dean |first=Jenna |date=2019-11-22 |title=Nested Loops |url=https://medium.com/swlh/nested-loops-ee1dbb9fc8ab |access-date=2024-03-14 |website=The Startup |language=en}}</ref> In [[functional programming]] languages, such as [[Haskell]] and [[Scheme (programming language)|Scheme]], both [[Recursion (computer science)|recursive]] and [[Fixed point combinator|iterative]] processes are expressed with [[Tail recursion|tail recursive]] procedures instead of looping constructs that are syntactic. === Count-controlled loops === {{main article|For loop}} Most programming languages have constructions for repeating a loop a certain number of times. In most cases counting can go downwards instead of upwards and step sizes other than 1 can be used. {| class="wikitable" | FOR I = 1 TO N xxx NEXT I | '''for''' I := 1 '''to''' N '''do''' '''begin''' xxx '''end'''; |- | DO I = 1,N xxx END DO | '''for''' ( I=1; I<=N; ++I ) { xxx } |} In these examples, if N < 1 then the body of loop may execute once (with I having value 1) or not at all, depending on the programming language. In many programming languages, only integers can be reliably used in a count-controlled loop. Floating-point numbers are represented imprecisely due to hardware constraints, so a loop such as<br /> '''for''' X := 0.1 '''step''' 0.1 '''to''' 1.0 '''do''' might be repeated 9 or 10 times, depending on rounding errors and/or the hardware and/or the compiler version. Furthermore, if the increment of X occurs by repeated addition, accumulated rounding errors may mean that the value of X in each iteration can differ quite significantly from the expected sequence 0.1, 0.2, 0.3, ..., 1.0. === Condition-controlled loops === {{main article|While loop|Do while loop}} Most programming languages have constructions for repeating a loop until some condition changes. Some variations test the condition at the start of the loop; others test it at the end. If the test is at the start, the body may be skipped completely; if it is at the end, the body is always executed at least once. {| class="wikitable" | DO WHILE (test) xxx LOOP | '''repeat''' xxx '''until''' test; |- | '''while''' (test) { xxx } | '''do''' xxx '''while''' (test); |} A [[control break]] is a value change detection method used within ordinary loops to trigger processing for groups of values. Values are monitored within the loop and a change diverts program flow to the handling of the group event associated with them. DO UNTIL (End-of-File) IF new-zipcode <> current-zipcode display_tally(current-zipcode, zipcount) current-zipcode = new-zipcode zipcount = 0 ENDIF zipcount++ LOOP === Collection-controlled loops === {{main article|Foreach loop|l1=Foreach}} Several programming languages (e.g., [[Ada (programming language)|Ada]], [[D (programming language)|D]], [[C++11]], [[Smalltalk]], [[PHP]], [[Perl]], [[Object Pascal]], [[Java (programming language)|Java]], [[C Sharp (programming language)|C#]], [[MATLAB]], [[Visual Basic]], [[Ruby (programming language)|Ruby]], [[Python (programming language)|Python]], [[JavaScript]], [[Fortran 95]] and later) have special constructs which allow implicit looping through all elements of an array, or all members of a set or collection. someCollection '''do''': [:eachElement |xxx]. '''for''' Item '''in''' Collection '''do''' '''begin''' xxx '''end'''; '''foreach''' (item; myCollection) { xxx } '''foreach''' someArray { xxx } '''foreach''' ($someArray as $k => $v) { xxx } Collection<String> coll; '''for''' (String s : coll) {} '''foreach''' ('''string''' s '''in''' myStringCollection) { xxx } someCollection | ForEach-Object { $_ } '''forall''' ( index = first:last:step... ) [[Scala (programming language)|Scala]] has [[Scala (programming language)#For-expressions|for-expressions]], which generalise collection-controlled loops, and also support other uses, such as [[asynchronous programming]]. [[Haskell]] has do-expressions and comprehensions, which together provide similar function to for-expressions in Scala. === General iteration === General iteration constructs such as C's <code>for</code> statement and [[Common Lisp]]'s <code>do</code> form can be used to express any of the above sorts of loops, and others, such as looping over some number of collections in parallel. Where a more specific looping construct can be used, it is usually preferred over the general iteration construct, since it often makes the purpose of the expression clearer. === Infinite loops === {{main article|Infinite loop}} [[Infinite loop]]s are used to assure a program segment loops forever or until an exceptional condition arises, such as an error. For instance, an event-driven program (such as a [[Server (computing)|server]]) should loop forever, handling events as they occur, only stopping when the process is terminated by an operator. Infinite loops can be implemented using other control flow constructs. Most commonly, in unstructured programming this is jump back up (goto), while in structured programming this is an indefinite loop (while loop) set to never end, either by omitting the condition or explicitly setting it to true, as <code>while (true) ...</code>. Some languages have special constructs for infinite loops, typically by omitting the condition from an indefinite loop. Examples include Ada (<code>loop ... end loop</code>),<ref>[[b:Ada Programming/Control#Endless Loop|Ada Programming: Control: Endless Loop]]</ref> Fortran (<code>DO ... END DO</code>), Go (<code>for { ... }</code>), and Ruby (<code>loop do ... end</code>). Often, an infinite loop is unintentionally created by a programming error in a condition-controlled loop, wherein the loop condition uses variables that never change within the loop. === Continuation with next iteration === Sometimes within the body of a loop there is a desire to skip the remainder of the loop body and continue with the next iteration of the loop. Some languages provide a statement such as <code>continue</code> (most languages), <code>skip</code>,<ref>{{cite web |title=What is a loop and how we can use them? |url=http://www.megacpptutorials.com/2012/12/what-is-loop.html |access-date=2020-05-25 |url-status=dead |archive-date=2020-07-28 |archive-url=https://web.archive.org/web/20200728081722/http://www.megacpptutorials.com/2012/12/what-is-loop.html }}</ref> <code>cycle</code> (Fortran), or <code>next</code> (Perl and Ruby), which will do this. The effect is to prematurely terminate the innermost loop body and then resume as normal with the next iteration. If the iteration is the last one in the loop, the effect is to terminate the entire loop early. === Redo current iteration === Some languages, like Perl<ref>{{cite web|title=redo - perldoc.perl.org|url=https://perldoc.perl.org/functions/redo.html|access-date=2020-09-25|website=perldoc.perl.org}}</ref> and Ruby,<ref>{{cite web|title=control_expressions - Documentation for Ruby 2.4.0|url=https://docs.ruby-lang.org/en/2.4.0/syntax/control_expressions_rdoc.html|access-date=2020-09-25|website=docs.ruby-lang.org}}</ref> have a <code>redo</code> statement that restarts the current iteration from the start. === Restart loop === Ruby has a <code>retry</code> statement that restarts the entire loop from the initial iteration.<ref>{{cite web|title=control_expressions - Documentation for Ruby 2.3.0|url=https://docs.ruby-lang.org/en/2.3.0/syntax/control_expressions_rdoc.html|access-date=2020-09-25|website=docs.ruby-lang.org}}</ref> === Early exit from loops === When using a count-controlled loop to search through a table, it might be desirable to stop searching as soon as the required item is found. Some programming languages provide a statement such as <code>break</code> (most languages), <code>Exit</code> (Visual Basic), or <code>last</code> (Perl), which effect is to terminate the current loop immediately, and transfer control to the statement immediately after that loop. Another term for early-exit loops is [[loop-and-a-half]]. The following example is done in [[Ada (programming language)|Ada]] which supports both ''early exit from loops'' and ''[[Control flow#Loop with test in the middle|loops with test in the middle]]''. Both features are very similar and comparing both code snippets will show the difference: ''early exit'' must be combined with an '''if''' statement while a ''condition in the middle'' is a self-contained construct. <syntaxhighlight lang="ada"> with Ada.Text IO; with Ada.Integer Text IO; procedure Print_Squares is X : Integer; begin Read_Data : loop Ada.Integer Text IO.Get(X); exit Read_Data when X = 0; Ada.Text IO.Put (X * X); Ada.Text IO.New_Line; end loop Read_Data; end Print_Squares; </syntaxhighlight> [[Python (programming language)|Python]] supports conditional execution of code depending on whether a loop was exited early (with a <code>break</code> statement) or not by using an else-clause with the loop. For example, <syntaxhighlight lang="python"> for n in set_of_numbers: if isprime(n): print("Set contains a prime number") break else: print("Set did not contain any prime numbers") </syntaxhighlight> The <code>else</code> clause in the above example is linked to the <code>for</code> statement, and not the inner <code>if</code> statement. Both Python's <code>for</code> and <code>while</code> loops support such an else clause, which is executed only if early exit of the loop has not occurred. Some languages support breaking out of nested loops; in theory circles, these are called multi-level breaks. One common use example is searching a multi-dimensional table. This can be done either via multilevel breaks (break out of ''N'' levels), as in bash<ref>Advanced Bash Scripting Guide: [http://tldp.org/LDP/abs/html/loopcontrol.html 11.3. Loop Control]</ref> and PHP,<ref>PHP Manual: "[http://php.net/manual/en/control-structures.break.php break]"</ref> or via labeled breaks (break out and continue at given label), as in Go, Java and Perl.<ref>perldoc: [http://perldoc.perl.org/functions/last.html last]</ref> Alternatives to multilevel breaks include single breaks, together with a state variable which is tested to break out another level; exceptions, which are caught at the level being broken out to; placing the nested loops in a function and using return to effect termination of the entire nested loop; or using a label and a goto statement. C does not include a multilevel break, and the usual alternative is to use a goto to implement a labeled break.<ref>comp.lang.c FAQ list · "[http://c-faq.com/misc/multibreak.html Question 20.20b]"</ref> Python does not have a multilevel break or continue – this was proposed in [https://www.python.org/dev/peps/pep-3136/ PEP 3136], and rejected on the basis that the added complexity was not worth the rare legitimate use.<ref>[http://mail.python.org/pipermail/python-3000/2007-July/008663.html <nowiki>[</nowiki>Python-3000<nowiki>]</nowiki> Announcing PEP 3136], Guido van Rossum</ref> The notion of multi-level breaks is of some interest in [[theoretical computer science]], because it gives rise to what is today called the ''Kosaraju hierarchy''.<ref name=kozen>{{cite book |first=Dexter |last=Kozen |date=2008 |chapter=The Böhm–Jacopini Theorem is False, Propositionally |title=Mathematics of Program Construction |series=Lecture Notes in Computer Science |doi=10.1007/978-3-540-70594-9_11 |volume=5133 |pages=177–192 |isbn=978-3-540-70593-2 |url=http://www.cs.cornell.edu/~kozen/papers/BohmJacopini.pdf |citeseerx=10.1.1.218.9241}}</ref> In 1973 [[S. Rao Kosaraju]] refined the [[structured program theorem]] by proving that it is possible to avoid adding additional variables in structured programming, as long as arbitrary-depth, multi-level breaks from loops are allowed.<ref>Kosaraju, S. Rao. "Analysis of structured programs," Proc. Fifth Annual ACM Syrup. Theory of Computing, (May 1973), 240-252; also in J. Computer and System Sciences, 9, 3 (December 1974). cited by {{cite journal |last=Knuth |first=Donald |author-link=Donald Knuth |year=1974 |title=Structured Programming with go to Statements |journal=Computing Surveys |volume=6 |issue=4 |pages=261–301 |doi=10.1145/356635.356640 |citeseerx=10.1.1.103.6084 |s2cid=207630080 }}</ref> Furthermore, Kosaraju proved that a strict hierarchy of programs exists: for every integer ''n'', there exists a program containing a multi-level break of depth ''n'' that cannot be rewritten as a program with multi-level breaks of depth less than ''n'' without introducing added variables.<ref name="kozen"/> One can also <code>return</code> out of a subroutine executing the looped statements, breaking out of both the nested loop and the subroutine. There are other [[#Proposed control structures|proposed control structures]] for multiple breaks, but these are generally implemented as exceptions instead. In his 2004 textbook, [[David Watt (computer scientist)|David Watt]] uses Tennent's notion of [[S-algol|sequencer]] to explain the similarity between multi-level breaks and return statements. Watt notes that a class of sequencers known as ''escape sequencers'', defined as "sequencer that terminates execution of a textually enclosing command or procedure", encompasses both breaks from loops (including multi-level breaks) and return statements. As commonly implemented, however, return sequencers may also carry a (return) value, whereas the break sequencer as implemented in contemporary languages usually cannot.<ref name="WattFindlay2004b">{{cite book|author1=David Anthony Watt|author2=William Findlay| title=Programming language design concepts| year=2004| publisher=John Wiley & Sons|isbn=978-0-470-85320-7|pages=215–221}}</ref> === Loop variants and invariants === [[Loop variant]]s and [[loop invariant]]s are used to express correctness of loops.<ref>{{cite book| last=Meyer| first=Bertrand| title=Eiffel: The Language| year=1991| publisher=Prentice Hall|pages=129–131}}</ref> In practical terms, a loop variant is an integer expression which has an initial non-negative value. The variant's value must decrease during each loop iteration but must never become negative during the correct execution of the loop. Loop variants are used to guarantee that loops will terminate. A loop invariant is an assertion which must be true before the first loop iteration and remain true after each iteration. This implies that when a loop terminates correctly, both the exit condition and the loop invariant are satisfied. Loop invariants are used to monitor specific properties of a loop during successive iterations. Some programming languages, such as [[Eiffel (programming language)|Eiffel]] contain native support for loop variants and invariants. In other cases, support is an add-on, such as the [[Java Modeling Language]]'s specification for [http://www.eecs.ucf.edu/~leavens/JML//jmlrefman/jmlrefman_12.html#SEC168 loop statements] in [[Java (programming language)|Java]]. === Loop sublanguage === Some [[Lisp (programming language)|Lisp]] dialects provide an extensive sublanguage for describing Loops. An early example can be found in Conversional Lisp of [[Interlisp]]. [[Common Lisp]]<ref>{{cite web|title=Common Lisp LOOP macro| url=http://www.lispworks.com/documentation/HyperSpec/Body/m_loop.htm}}</ref> provides a Loop macro which implements such a sublanguage. === Loop system cross-reference table ===<!-- See also Category:Programming language comparisons. --> {| class="wikitable" |- ! rowspan=2 |[[Programming language]] ! colspan=3 | conditional ! colspan=4 | loop ! rowspan=2 | early exit ! rowspan=2 | loop continuation ! rowspan=2 | redo ! rowspan=2 | retry ! colspan=2 | correctness facilities |- ! begin ! middle ! end ! count ! collection ! general ! infinite {{ref_label|loop_infinite|1|a}} ! variant ! invariant |- | [[Ada (programming language)|Ada]] | {{yes}} | {{yes}} | {{yes}} | {{yes}} | {{partial|arrays}} | {{no}} | {{yes}} | {{yes|deep nested}} | {{no}} | | | | |- | [[APL (programming language)|APL]] | {{yes}} | {{no}} | {{yes}} | {{yes}} | {{yes}} | {{yes}} | {{yes}} | {{partial|deep nested}} {{ref_label|deep_break_c|3|a}} | {{yes}} | {{no}} | {{no}} | | |- | [[C (programming language)|C]] | {{yes}} | {{no}} | {{yes}} | {{no}} {{ref_label|loop_for|2|a}} | {{no}} | {{yes}} | {{no}} | {{partial|deep nested}} {{ref_label|deep_break_c|3|a}} | {{partial|deep nested}} {{ref_label|deep_break_c|3|a}} | {{no}} | | | |- | [[C++]] | {{yes}} | {{no}} | {{yes}} | {{no}} {{ref_label|loop_for|2|b}} | {{yes}} {{ref_label|loop_foreach|9|a}} | {{yes}} | {{no}} | {{partial|deep nested}} {{ref_label|deep_break_c|3|b}} | {{partial|deep nested}} {{ref_label|deep_break_c|3|a}} | {{no}} | | | |- | [[C Sharp (programming language)|C#]] | {{yes}} | {{no}} | {{yes}} | {{no}} {{ref_label|loop_for|2|c}} | {{yes}} | {{yes}} | {{no}} | {{partial|deep nested}} {{ref_label|deep_break_c|3|c}} | {{partial|deep nested}} {{ref_label|deep_break_c|3|a}} | | | | |- | [[COBOL]] | {{yes}} | {{no}} | {{yes}} | {{yes}} | {{no}} | {{yes}} | {{no}} | {{partial|deep nested}} {{ref_label|cobol_deep_exit|15|a}} | {{partial|deep nested}} {{ref_label|cobol_deep_exit|14|a}} | {{no}} | | | |- | [[Common Lisp]] | {{yes}} | {{yes}} | {{yes}} | {{yes}} | {{partial|builtin only}} {{ref_label|cl_sequence_type|16|a}} | {{yes}} | {{yes}} | {{yes|deep nested}} | {{no}} | | | | |- | [[D (programming language)|D]] | {{yes}} | {{no}} | {{yes}} | {{yes}} | {{yes}} | {{yes}} | {{yes}}{{ref_label|DInfinite|14|a}} | {{yes|deep nested}} | {{yes|deep nested}} | {{no}} | | | |- | [[Eiffel (programming language)|Eiffel]] | {{yes}} | {{no}} | {{no}} | {{yes}} {{ref_label|count_loop_eiffel|10|a}} | {{yes}} | {{yes}} | {{no}} | {{partial|one level}} {{ref_label|count_loop_eiffel|10|a}} | {{no}} | {{no}} | {{no}} {{ref_label|retry_in_eiffel|11|a}} | {{partial|integer only}} {{ref_label|integer_variant|13|a}} | {{yes}} |- | [[F Sharp (programming language)|F#]] | {{yes}} | {{no}} | {{no}} | {{yes}} | {{yes}} | {{no}} | {{no}} | {{no}} {{ref_label|deep_break_e|6|b}} | {{no}} | {{no}} | | | |- | [[FORTRAN 77]] | {{yes}} | {{no}} | {{no}} | {{yes}} | {{no}} | {{no}} | {{no}} | {{partial|one level}} | {{yes }} | {{no}} | {{no}} | | |- | [[Fortran 90]] | {{yes}} | {{no}} | {{no}} | {{yes}} | {{no}} | {{no}} | {{yes}} | {{yes|deep nested}} | {{yes|deep nested}} | {{no}} | {{no}} | | |- | [[Fortran 95]] and later | {{yes}} | {{no}} | {{no}} | {{yes}} | {{partial|arrays}} | {{no}} | {{yes}} | {{yes|deep nested}} | {{yes|deep nested}} | {{no}} | {{no}} | | |- | [[Go (programming language)|Go]] | {{yes}} | {{no}} | {{no}} | {{yes}} | {{partial|builtin only}} | {{yes}} | {{yes}} | {{yes|deep nested}} | {{yes|deep nested}} | {{no}} | | | |- | [[Haskell]] | {{no}} | {{no}} | {{no}} | {{no}} | {{yes}} | {{no}} | {{yes}} | {{no}} {{ref_label|deep_break_e|6|a}} | {{no}} | {{no}} | | | |- | [[Java (programming language)|Java]] | {{yes}} | {{no}} | {{yes}} | {{no}} {{ref_label|loop_for|2|d}} | {{yes}} | {{yes}} | {{no}} | {{yes|deep nested}} | {{yes|deep nested}} | {{no}} | |{{yes|non-native}} {{ref_label|requires_JML|12|a}} |{{yes|non-native}} {{ref_label|requires_JML|12|a}} |- | [[JavaScript]] | {{yes}} | {{no}} | {{yes}} | {{no}} {{ref_label|loop_for|2|g}} | {{yes}} | {{yes}} | {{no}} | {{yes|deep nested}} | {{yes|deep nested}} | {{no}} | | |- | Natural | {{yes}} | {{yes}} | {{yes}} | {{yes}} | {{no}} | {{yes}} | {{yes}} | {{yes}} | {{yes}} | {{yes}} | {{no}} | |- | [[OCaml]] | {{yes}} | {{no}} | {{no}} | {{yes}} | {{yes|arrays,lists}} | {{no}} | {{no}} | {{no}} {{ref_label|deep_break_e|6|b}} | {{no}} | {{no}} | | | |- | [[PHP]] | {{yes}} | {{no}} | {{yes}} | {{no}} {{ref_label|loop_for|2|e}} {{ref_label|loop_for|5|b}} | {{yes}} {{ref_label|loop_phpforeach|4|a}} | {{yes}} | {{no}} | {{yes|deep nested}} | {{yes|deep nested}} | {{no}} | | | |- | [[Perl]] | {{yes}} | {{no}} | {{yes}} | {{no}} {{ref_label|loop_for|2|f}} {{ref_label|loop_for|5|c}} | {{yes}} | {{yes}} | {{no}} | {{yes|deep nested}} | {{yes|deep nested}} | {{yes}} | | | |- | [[Python (programming language)|Python]] | {{yes}} | {{no}} | {{no}} | {{no}} {{ref_label|loop_range|5|a}} | {{yes}} | {{no}} | {{no}} | {{partial|deep nested}} {{ref_label|deep_break_e|6|c}} | {{partial|deep nested}} {{ref_label|deep_break_e|6|c}} | {{no}} | | | |- | [[Rebol]] | {{no}} {{ref_label|while|7|a}} | {{yes}} | {{yes}} | {{yes}} | {{yes}} | {{no}} {{ref_label|user|8|a}} | {{yes}} | {{partial|one level}} {{ref_label|deep_break_e|6|d}} | {{no}} | {{no}} | | | |- | [[Ruby (programming language)|Ruby]] | {{yes}} | {{no}} | {{yes}} | {{yes}} | {{yes}} | {{no}} | {{yes}} | {{partial|deep nested}} {{ref_label|deep_break_e|6|e}} | {{partial|deep nested}} {{ref_label|deep_break_e|6|e}} | {{yes}} | {{yes}} | | |- | [[Standard ML]] | {{yes}} | {{no}} | {{no}} | {{no}} | {{yes|arrays,lists}} | {{no}} | {{no}} | {{no}} {{ref_label|deep_break_e|6|b}} | {{no}} | {{no}} | | | |- | [[Visual Basic .NET]] | {{yes}} | {{no}} | {{yes}} | {{yes}} | {{yes}} | {{no}} | {{yes}} | {{partial|one level per type of loop}} | {{partial|one level per type of loop}} | | | | |- | [[PowerShell]] | {{yes}} | {{no}} | {{yes}} | {{no}} {{ref_label|loop_for|2|h}} | {{yes}} | {{yes}} | {{no}} | {{dunno}} | {{yes}} | | | | |} # {{note label|loop_infinite|1|a}} <code>while (true)</code> does not count as an infinite loop for this purpose, because it is not a dedicated language structure. # {{note label|loop for|2|a}}{{note_label|loop_for|2|b}}{{note_label|loop_for|2|c}}{{note_label|loop_for|2|d}}{{note_label|loop_for|2|e}}{{note_label|loop_for|2|f}}{{note_label|loop_for|2|g}}{{note_label|loop_for|2|h}} C's <code>for (''init''; ''test''; ''increment'')</code> loop is a general loop construct, not specifically a counting one, although it is often used for that. # {{note label|deep_break_c|3|a}}{{note label|deep_break_c|3|b}}{{note label|deep_break_c|3|c}} Deep breaks may be accomplished in APL, C, C++ and C# through the use of labels and gotos. # {{note label|loop_phpforeach|4|a}} Iteration over objects was [http://www.php.net/manual/en/language.oop5.iterations.php added] in PHP 5. # {{note label|loop_range|5|a}}{{note label|loop_range|5|b}}{{note label|loop_range|5|c}} A counting loop can be simulated by iterating over an incrementing list or generator, for instance, Python's <code>range()</code>. # {{note label|deep_break_e|6|a}}{{note label|deep_break_e|6|b}}{{note label|deep_break_e|6|c}}{{note label|deep_break_e|6|d}}{{note label|deep_break_e|6|e}} Deep breaks may be accomplished through the use of exception handling. # {{note_label|while|7|a}} There is no special construct, since the <code>while</code> function can be used for this. # {{note_label|user|8|a}} There is no special construct, but users can define general loop functions. # {{note_label|loop_foreach|9|a}} The [[C++11]] standard introduced the [[C++11#Range-based for loop|range-based for]]. In the [[Standard Template Library|STL]], there is a <code>std::for_each</code> [[template (programming)|template]] function which can iterate on STL [[Container (data structure)|containers]] and call a [[unary function]] for each element.<ref>[http://www.sgi.com/tech/stl/for_each.html for_each]. Sgi.com. Retrieved on 2010-11-09.</ref> The functionality also can be constructed as [[C preprocessor#Macro definition and expansion|macro]] on these containers.<ref>[http://boost-sandbox.sourceforge.net/libs/foreach/doc/html/ Chapter 1. Boost.Foreach] {{Webarchive|url=https://web.archive.org/web/20100129070613/http://boost-sandbox.sourceforge.net/libs/foreach/doc/html/ |date=2010-01-29}}. Boost-sandbox.sourceforge.net (2009-12-19). Retrieved on 2010-11-09.</ref> # {{note_label|count_loop_eiffel|10|a}} Count-controlled looping is effected by iteration across an integer interval; early exit by including an additional condition for exit. # {{note_label|retry_in_eiffel|11|a}} Eiffel supports a reserved word <code>retry</code>, however it is used in [[Exception handling#Exception handling based on design by contract|exception handling]], not loop control. # {{note_label|requires_JML|12|a}} Requires [[Java Modeling Language]] (JML) behavioral interface specification language. # {{note_label|integer_variant|13|a}} Requires loop variants to be integers; transfinite variants are not supported. [http://archive.eiffel.com/doc/faq/variant.html] # {{note_label|DInfinite|13|a}} D supports infinite collections, and the ability to iterate over those collections. This does not require any special construct. # {{note_label|cobol_deep_exit|14|a}} Deep breaks can be achieved using <code>GO TO</code> and procedures. # {{note_label|cl_sequence_type|14|a}} Common Lisp predates the concept of generic collection type.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)