Difference between revisions of "Branch"

From Free Pascal wiki
m (Usage: insert link to Case Compiler Optimization)
(Types of branches: mention “case of <string> …” is legal too)
Line 17: Line 17:
  
 
<syntaxhighlight lang="pascal" inline>Case … of</syntaxhighlight> on the other hand is n-ary, where n&nbsp;is greater than one.
 
<syntaxhighlight lang="pascal" inline>Case … of</syntaxhighlight> on the other hand is n-ary, where n&nbsp;is greater than one.
Unlike <syntaxhighlight lang="pascal" inline>if</syntaxhighlight> the supplied expression the program flow depends on can (and has to) be of ''any'' integral type, e.&#8239;g. an [[Integer|integer]] or [[Enum Type|enumeration]].
+
Unlike <syntaxhighlight lang="pascal" inline>if</syntaxhighlight> the supplied expression the program flow depends on can (and has to) be of ''any'' integral type, e.&#8239;g. an [[Integer|integer]] or [[Enum Type|enumeration]] (in FreePascal also [[String|strings]] are legal).
  
 
== Usage ==
 
== Usage ==
 
<syntaxhighlight lang="pascal" inline>Case … of</syntaxhighlight> branches should be considered where appropriate since they [[Case Compiler Optimization|aide compiler optimizations]].
 
<syntaxhighlight lang="pascal" inline>Case … of</syntaxhighlight> branches should be considered where appropriate since they [[Case Compiler Optimization|aide compiler optimizations]].
 
By using them [[FPC]] may re-arrange statements, the given alternatives, in order to optimize for speed or size.
 
By using them [[FPC]] may re-arrange statements, the given alternatives, in order to optimize for speed or size.

Revision as of 10:20, 22 June 2020

Deutsch (de) English (en)

A branch, also called conditional or conditional statement, is a statement that is executed depending on an expression’s value. Usually this expression depends on the program’s state.

Types of branches

Pascal knows two types of branches:

They differ in how many alternatives can be named, how many “routes” could be taken.

In an if  then  branch there exists exactly one alternative. It is either executed or not. In conjunction with the word else one can specify exactly two “paths” with regard to program flow. Due to its binary nature, if  then requires a Boolean expression to be evaluated.

Case  of on the other hand is n-ary, where n is greater than one. Unlike if the supplied expression the program flow depends on can (and has to) be of any integral type, e. g. an integer or enumeration (in FreePascal also strings are legal).

Usage

Case  of branches should be considered where appropriate since they aide compiler optimizations. By using them FPC may re-arrange statements, the given alternatives, in order to optimize for speed or size.