Difference between revisions of "Loops"

From Free Pascal wiki
Jump to navigationJump to search
m (→‎types: syntax)
(→‎types: add statement that loop types are interchangeable [to some extent])
 
(2 intermediate revisions by the same user not shown)
Line 7: Line 7:
 
* loop head, and a
 
* loop head, and a
 
* loop body.
 
* loop body.
The loop body is a statement, and the head contains a [[Boolean Expressions|Boolean expression]] that determines whether the loop body is executed (again).
+
The loop body is a statement, and the head (possibly implicitly) contains a [[Boolean Expressions|Boolean expression]] that determines whether the loop body is executed (again).
 +
Every time the statements in the loop body are executed, an ''iteration'' occurs.
  
 
Loops are particularly useful as a programming construct, since the loop body is only inserted once in the final program.
 
Loops are particularly useful as a programming construct, since the loop body is only inserted once in the final program.
The so-called “loop unrolling” optimization may ''copy'' the loop body multiple times anyway, but still you do not need to literally repeat the statements in your [[Source code|source code]].
+
The so-called “loop unrolling” [[Compiler|compiler]] optimization may ''copy'' the loop body multiple times anyway, but still you do not need to literally repeat the statements in your [[Source code|source code]].
 +
Some processors perform particularly well (fast) if a repeating series of instructions occupies a quite ''small'' chunk of memory.
  
 
== types ==
 
== types ==
Line 17: Line 19:
 
* conditional loops
 
* conditional loops
 
** [[While|<syntaxhighlight lang="pascal" inline>while … do</syntaxhighlight>]], and
 
** [[While|<syntaxhighlight lang="pascal" inline>while … do</syntaxhighlight>]], and
** [[Repeat|<syntaxhighlight lang="pascal" inline>repeat … until</syntaxhighlight>]] where the loop head appears at the foot.
+
** [[Repeat|<syntaxhighlight lang="pascal" inline>repeat … until</syntaxhighlight>]] where the loop head appears at the tail.
  
The [[FPC]] also supports [[for-in loop|<syntaxhighlight lang="pascal" inline>for … in … do</syntaxhighlight>]] loops.
+
The [[FPC]] also supports [[for-in loop|<syntaxhighlight lang="pascal" inline>for … in … do</syntaxhighlight>]] loops, which are similar to counting loops.
  
== comparative remark ==
+
If the loop body has a predictable number of iterations, the loop can be written with any loop type, but a counting loop is usually the most reasonable choice.
 +
Likewise, conditional loops are interchangeable, too, but in any given situation either one is more suitable.
 +
 
 +
== comparative remarks ==
 
Unlike in some programming languages, in Pascal a loop itself is a statement; it does not yield a value.
 
Unlike in some programming languages, in Pascal a loop itself is a statement; it does not yield a value.
 +
Also, a loop body does not create a new [[Scope|scope]].
  
 
== see also ==
 
== see also ==

Latest revision as of 22:25, 26 January 2021

Deutsch (de) English (en)

A loop control structure repeats a statement as long as a certain condition is met.

properties

A loop is sectioned into a

  • loop head, and a
  • loop body.

The loop body is a statement, and the head (possibly implicitly) contains a Boolean expression that determines whether the loop body is executed (again). Every time the statements in the loop body are executed, an iteration occurs.

Loops are particularly useful as a programming construct, since the loop body is only inserted once in the final program. The so-called “loop unrolling” compiler optimization may copy the loop body multiple times anyway, but still you do not need to literally repeat the statements in your source code. Some processors perform particularly well (fast) if a repeating series of instructions occupies a quite small chunk of memory.

types

Pascal defines

The FPC also supports for  in  do loops, which are similar to counting loops.

If the loop body has a predictable number of iterations, the loop can be written with any loop type, but a counting loop is usually the most reasonable choice. Likewise, conditional loops are interchangeable, too, but in any given situation either one is more suitable.

comparative remarks

Unlike in some programming languages, in Pascal a loop itself is a statement; it does not yield a value. Also, a loop body does not create a new scope.

see also