Difference between revisions of "Loops"

From Free Pascal wiki
Jump to navigationJump to search
(English translation of German page)
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
{{LanguageBar}}
 
{{LanguageBar}}
  
A loop is a sequence of instructions that is repeated until a certain condition is reached. An operation is done (eg getting an item of data and changing it) and then some condition is checked (eg whether a counter has reached a specified number).
+
A '''loop''' control structure repeats a [[statement]] as long as a certain condition is met.
  
A loop has a loop head, a loop foot and a loop body.
+
== properties ==
 +
A loop is sectioned into a
 +
* loop head, and a
 +
* 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 conditions under which a loop is run are either in the loop head or in the loop foot.
+
Loops are particularly useful as a programming construct, since the loop body is only inserted once in the final program.
The instructions to be processed are in the loop body.
+
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]].
  
Free Pascal has four types of loops:
+
== types ==
 +
[[Standard Pascal|Pascal]] defines
 +
* counting loops [[For|<syntaxhighlight lang="pascal" inline>for … to|downto do</syntaxhighlight>]]
 +
* conditional loops
 +
** [[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.
  
    Head-controlled Counting loop [[For|For ...]]
+
The [[FPC]] also supports [[for-in loop|<syntaxhighlight lang="pascal" inline>for … in … do</syntaxhighlight>]] loops.
    Head-controlled loop [[While|While ...]]
 
    [[Repeat]] foot controlled loop
 
    Head-controlled For-In Loop [[For|For ... In ...]]
 
  
== See also ==
+
== comparative remark ==
 +
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 ==
 
* [[Pascal basics]]
 
* [[Pascal basics]]
 
+
* [[Main Loop Hooks]]
  
 
[[Category:Pascal]]
 
[[Category:Pascal]]
 
[[Category:Control Structures]]
 
[[Category:Control Structures]]

Revision as of 20:48, 20 August 2020

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 contains a Boolean expression that determines whether the loop body is executed (again).

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.

types

Pascal defines

The FPC also supports for  in  do loops.

comparative remark

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