FazBrowse Wikipedia Viewer | Main Page |
Search Wikipedia:
|
Viewing: https://en.wikipedia.org/wiki/Program_loops

Control flow

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"); }

Control

edit

Many control commands have been devised for programming languages. This section describes notable constructs; it is organized by functionality.

Functions

edit

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.

if-then-else
Like if-then, but with a second action to be performed if the condition is false. In a language that does not include the when and if(a>0){ puts("yes"); }else{ puts("no"); }

In bash:

(princ (if(pluspa) "yes" "no"))

Multiway branch

edit

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:

actionOnA;\n when 'x' => actionOnX;\n when 'y' | 'z' => actionOnYandZ;\n when others => actionOnNoMatch;\nend;\n"}}'>case someChar is when 'a' => actionOnA; when 'x' => actionOnX; when 'y' | 'z' => actionOnYandZ; when others => actionOnNoMatch; end;

In C:

(casesome-char ((#\a)action-on-a) ((#\x)action-on-x) ((#\y#\z)action-on-y-and-z) (elseaction-on-no-match))

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.

Numeric

edit
FORI=1TON xxx NEXTI

Example in Pascal:

repeat xxx untiltest;

Example in C family of pre-test:

for keyword, the three-part C-style loop is a condition-based construct, not a numeric-based one. The second part, the condition, is evaluated before each loop, so the loop is pre-test. The first part is a place to initialize state, and the third part is for incrementing for the next iteration, but both aspects can be performed elsewhere. The following C code implements the logic of a numeric loop that iterates for i from 0 to n-1.

Some loop constructs enumerate the items of a collection; iterating for each item.

Example in Smalltalk:

foreach (item; myCollection) { xxx }

Example in TCL:

Collection<String>coll; for(Strings:coll){}

Example in C#:

forall(index=first:last:step...)

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.

Infinite

edit
n⁠ times, but the second part is only executed loop Get(X); if X = 0 then exit; end if; DoSomething(X); end loop;

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:

0) {\n // ...\n}\n"}}'>while(read_string(s),strlen(s)>0){ // ... }

Loop variants and invariants

edit

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.

Loop sublanguage

edit

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.

Loop system cross-reference table

edit
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.
  • Numeric looping is effected by iteration across an integer interval; early exit by including an additional condition for exit.
  • Requires loop variants to be integers; transfinite variants are not supported. Eiffel: Why loop variants are integers
  • Common Lisp predates the concept of generic collection type.
  • doSomething(), then control jumps from line 2 to 4 and the message "Caught runtime_error" is printed and then control jumps to after the throw, exception to introduce an exception handler and
    View original on Wikipedia | Back | FazBrowse Home