Difference between revisions of ";"

From Free Pascal wiki
m ((a clear all break instead of several (in fact any arbitrary number) of breaks). Thanks to Kai for showing me how to.)
(complete review)
Line 1: Line 1:
 
{{;}}
 
{{;}}
 
 
 
<div style="float:left; margin: 0 25px 20px 0; padding:50px; font-size:500%; font-family: Georgia; background-color: #f9f9f9; border: 2px solid #777777; clear:both;">;</div>
 
<div style="float:left; margin: 0 25px 20px 0; padding:50px; font-size:500%; font-family: Georgia; background-color: #f9f9f9; border: 2px solid #777777; clear:both;">;</div>
  
The semicolon [[ASCII]] or the unicode value is 59 ([[Hexadecimal]] $3B).
+
The '''semicolon''' <syntaxhighlight lang="pascal" enclose="none">;</syntaxhighlight> is used to ''separate'' statements, in contrast to other programming language where its purpose is to ''terminate'' a statement.
 +
In a block a semicolon without a preceding (qualified) statement indicates an '''empty statement'''.
  
The '''semicolon''', ''''';''''' is used to ''separate'' statements (unlike other programming languages where it is used to ''terminate'' a statement). It is mandatory for most statements, voluntary in some statements, and forbidden in one particular statement.
+
== necessity ==
 +
Since language constructs only in their entirety constitute statements, semicolons may not split their components.
 +
Most notably <syntaxhighlight lang="pascal" enclose="none">;</syntaxhighlight> can not appear immediately before an [[Else|<syntaxhighlight lang="pascal" enclose="none">else</syntaxhighlight>]] that is part of an [[If and Then|<syntaxhighlight lang="pascal" enclose="none">if … then</syntaxhighlight> branch]].
 +
However, <syntaxhighlight lang="pascal" enclose="none">;</syntaxhighlight> in front of an [[End|<syntaxhighlight lang="pascal" enclose="none">end</syntaxhighlight>]] usually is not necessary.
  
(In order to explain usage, where Pascal keywords are referred to in the text of this article, they are shown in UPPER case. Program listings will use lower case.)
+
As a demonstration, that a single semicolon can make the difference, consider the following listings:
<br clear="all"/>
+
<syntaxhighlight lang="pascal" highlight="2">
 +
case c of
 +
0: if false then c := 42;
 +
else c := -1;
 +
end;
 +
</syntaxhighlight>
 +
If <syntaxhighlight lang="pascal" enclose="none">c</syntaxhighlight> is zero, it remains zero, but becomes <syntaxhighlight lang="pascal" enclose="none">-1</syntaxhighlight> otherwise.
 +
<syntaxhighlight lang="pascal" highlight="2">
 +
case c of
 +
0: if false then c := 42
 +
else c := -1;
 +
end;
 +
</syntaxhighlight>
 +
Here, <syntaxhighlight lang="pascal" enclose="none">c</syntaxhighlight> only becomes <syntaxhighlight lang="pascal" enclose="none">-1</syntaxhighlight> only if it's been zero before.
 +
As a consequence, and general advice, always put everything in compound statements (i.e. embrace your statements by [[Begin|<syntaxhighlight lang="pascal" enclose="none">begin</syntaxhighlight>]] and <syntaxhighlight lang="pascal" enclose="none">end</syntaxhighlight>) where it's allowed, in order to mitigate such issues.
  
The semicolon is generally mandatory, and is required after every statement.  The places where it is optional are
+
== other remarks ==
* before an [[End|END]] statement
+
In the [[ASCII]] character set the semicolon takes the value <syntaxhighlight lang="pascal" enclose="none">59</syntaxhighlight> ([[Hexadecimal|hexadecimal]] <syntaxhighlight lang="delphi" enclose="none">$3B</syntaxhighlight>).
<br>
 
A semicolon is forbidden
 
* immediately before an [[Else|ELSE]] statement that belongs to an if statement
 
<br>
 
Notice that a semicolon can occur immediately before an else statement that belongs to a case statement.
 
<br>
 
Consider the following:
 
<syntaxhighlight lang="pascal">
 
  case a of
 
    true: if b then DoB;
 
    else DoNotA;
 
  end;
 
</syntaxhighlight>
 
  
The else part belongs to the case statement.<br>
+
Historically empty statements were used in conjunction with [[Label|labels]].
DoNotA will be executed if none of the case labels (just one in this case) applies.
+
Originally labels can only defined where a statement exists.
<br>
+
If for instance a whole list of statements had to be bypassed, but no qualified statement followed thereafter, the empty statement still provided the possibility.
<br>
 
Now if you leave out the semicolon after DoB
 
<syntaxhighlight lang="pascal">
 
  case a of
 
    true: if b then DoB
 
    else DoNotB;
 
  end;
 
</syntaxhighlight>
 
The else statement now is part of the "if b".<br>
 
DoNotB will only be executed if a is true, and b is false.
 
  
 +
Also historically, [[Case|<syntaxhighlight lang="pascal" enclose="none">case</syntaxhighlight>-statements]] had to list all possible values the selector variable theoretically could have.
 +
Now, if a value or range did not imply any action, yet had to be listed inside the <syntaxhighlight lang="pascal" enclose="none">case</syntaxhighlight>-statement in order to fulfill this requirement, an empty statement is the shortest possible way to implement the situation.
  
 
{{Symbols}}
 
{{Symbols}}

Revision as of 01:42, 20 October 2018

English (en) suomi (fi) français (fr)

;

The semicolon ; is used to separate statements, in contrast to other programming language where its purpose is to terminate a statement. In a block a semicolon without a preceding (qualified) statement indicates an empty statement.

necessity

Since language constructs only in their entirety constitute statements, semicolons may not split their components. Most notably ; can not appear immediately before an else that is part of an if  then branch. However, ; in front of an end usually is not necessary.

As a demonstration, that a single semicolon can make the difference, consider the following listings:

	case c of
		0: if false then c := 42;
		else c := -1;
	end;

If c is zero, it remains zero, but becomes -1 otherwise.

	case c of
		0: if false then c := 42
		else c := -1;
	end;

Here, c only becomes -1 only if it's been zero before. As a consequence, and general advice, always put everything in compound statements (i.e. embrace your statements by begin and end) where it's allowed, in order to mitigate such issues.

other remarks

In the ASCII character set the semicolon takes the value 59 (hexadecimal $3B).

Historically empty statements were used in conjunction with labels. Originally labels can only defined where a statement exists. If for instance a whole list of statements had to be bypassed, but no qualified statement followed thereafter, the empty statement still provided the possibility.

Also historically, case-statements had to list all possible values the selector variable theoretically could have. Now, if a value or range did not imply any action, yet had to be listed inside the case-statement in order to fulfill this requirement, an empty statement is the shortest possible way to implement the situation.


navigation bar: topic: Pascal symbols
single characters

+ (plus)  •  - (minus)  •  * (asterisk)  •  / (slash)
= (equal)  •  > (greater than)  •  < (less than)
. (period)  •  : (colon)  •  ; (semi colon)
^ (hat)  •  @ (at)
$ (dollar sign)  •  & (ampersand)  •  # (hash)
' (single quote)

character pairs

<> (not equal)  •  <= (less than or equal)  •  := (becomes)  •  >= (greater than or equal)

 •  >< (symmetric difference)  •  // (double slash)