| FazBrowse Wikipedia Viewer | Main Page | | |
| Viewing: https://en.wikipedia.org/wiki/Program_loops |
| Contents |
|---|
| Control |
| Functions |
| Multiway branch |
| Numeric |
| Infinite |
| Loop variants and invariants |
| Loop sublanguage |
| Loop system cross-reference table |
In software, control flow (or flow of control) describes how execution progresses from one command to the next. In many contexts, such as machine code and an imperative programming language, control progresses sequentially (to the command located immediately after the currently executing command) except when a command transfers control to another point 10LETX=3 20PRINTX 30GOTO10
In many languages, a label is an alphanumeric identifier, usually appearing at the start of a line and immediately followed by a colon. For example, the following C code defines a label if(done){ printf("All done"); }else{ printf("Still workin' on it"); }
Many control commands have been devised for programming languages. This section describes notable constructs; it is organized by functionality.
A function provides for control flow in that when called, execution jumps to the start of the function's code and when it completes, control returns the calling point. In the following C code, control jumps from line 6 to 2 in order to call function then keyword, this can be called an if statement.
In bash:
A multiway branch jumps control based on matching values. There is usually a provision for a default action if no match is found. A switch statement can allow compiler optimizations, such as lookup tables. In dynamic languages, the cases may not be limited to constant expressions, and might extend to pattern matching, as in the shell script example on the right, where the *) implements the default case as a glob matching any string. Case logic can also be implemented in functional form, as in SQL's decode statement.
The following Pascal code shows a relatively simple switch statement. Pascal uses the casesomeCharof 'a':actionOnA; 'x':actionOnX; 'y','z':actionOnYandZ; elseactionOnNoMatch; end;
In Ada:
In C:
In Fortran:
In a loop, a sequence of statements—frequently called the loop body—gets re-executed (at least in part) after a conditional branch or jump to a previous step in the sequence. The number of times a loop gets executed usually depends on runtime state, for example: the body is executed once for each item of a collection (definite iteration), until a condition is met (indefinite iteration). Some loops, on the other hand, may repeat infinitely. It is possible for a loop to be written as an infinite loop but for loop exit to be controlled by a test within the loop body that transfers control outside the loop. Early exit from a loop may be supported via a break statement.
A loop inside the loop body of another loop is called a nested loop.
In a functional programming language, such as Haskell and Scheme, both recursive and iterative processes are expressed with tail recursive procedures instead of looping constructs that are syntactic.
Example in Pascal:
Example in C family of pre-test:
Some loop constructs enumerate the items of a collection; iterating for each item.
Example in Smalltalk:
Example in TCL:
Example in C#:
Scala has 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.
A more idiomatic style uses n, there exists a program containing a multi-level break of depth while(statement_1,statement_2,condition){ statement_a; statement_b; }
While legal, this is marginal, and it is primarily used, if at all, only for short modify-then-test cases, as in:
Loop variants and loop invariants are used to express correctness of loops.
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 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 loop statements in Java.
Some Lisp dialects provide an extensive sublanguage for describing Loops. An early example can be found in Conversional Lisp of Interlisp. Common Lisp provides a Loop macro which implements such a sublanguage.
| Programming language | Conditional | Loop | Early exit | Loop continuation | Redo | Retry | Correctness facilities | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Begin | Middle | End | Numeric | Collection | General | Infinite | Variant | Invariant | ||||||||||
| Ada | Yes | No | No | |||||||||||||||
| APL | Yes | Yes | No | |||||||||||||||
| C | Yes | No | deep nested | No | No | Yes | No | No | ||||||||||
| C# | Yes | Yes | deep nested | No | No | No | deep nested | Yes | builtin only Yes | Yes | Yes | Yesdeep nested | No | No | No | Yes | ||
| F# | No | No | Yes | Yes | No | No | No | No | deep nested | No | ||||||||
| Fortran 95 and later | No | No | deep nested | Yes | Yes | Yes | No | |||||||||||
| Haskell | No | No | Yes | No Yes | deep nested | Yes | No Yes | deep nested | No | |||||||||
| Kotlin | Yes | No | deep nested | No | Yes | No | Yes | No | ||||||||||
| OCaml | No | No | No No | builtin only | Yes | No Yes No | No | No | ||||||||||
| Perl | Yes | No | Yes | |||||||||||||||
| Python | No | Yes | deep nested | No | No | No | one level per type of loop | No | No Yes | No one level No | ||||||||
| Ruby | Yes | No | Yes | No | Yes | No | No | |||||||||||
| Standard ML | No | No | Yes | No | No | No | No | |||||||||||
| Visual Basic .NET | Yes | No | one level per type of loop | No | No | No | No | No | ||||||||||
| Zig | No | No | deep nested | No | There is no special construct, since the while function can be used for this. | |||||||||||||
| View original on Wikipedia | Back | FazBrowse Home |