Difference between revisions of ";"

From Free Pascal wiki
(link declaration)
 
(8 intermediate revisions by 2 users not shown)
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>
  
 +
The '''semicolon''' <syntaxhighlight lang="pascal" inline>;</syntaxhighlight> is used to
 +
* conclude a [[Declaration|declaration]],
 +
* conclude a constant, <syntaxhighlight lang="delphi" inline>resourceString</syntaxhighlight>, or [[Type|<syntaxhighlight lang="pascal" inline>type</syntaxhighlight>]] definition,
 +
* separate formal parameters in a routine signature,
 +
* separate a routine declaration from its attributes,
 +
* terminate the program header,
 +
* separate alternatives in variant records, and to
 +
* ''separate'' statements, in contrast to other programming language where its purpose is to ''terminate'' a statement.
  
<div style="float:left; margin: 0 25px 20px 0; padding:50px; font-size:500%; font-family: Georgia; background-color: #f9f9f9; border: 2px solid #777777;">;</div>
+
== statement separator ==
 +
=== necessity ===
 +
Since language constructs only in their entirety constitute statements, semicolons may not split their components.
 +
Most notably <syntaxhighlight lang="pascal" inline>;</syntaxhighlight> cannot appear immediately before an [[Else|<syntaxhighlight lang="pascal" inline>else</syntaxhighlight>]] that is part of an [[If and Then|<syntaxhighlight lang="pascal" inline>if … then</syntaxhighlight> branch]].
 +
However, <syntaxhighlight lang="pascal" inline>;</syntaxhighlight> in front of an [[End|<syntaxhighlight lang="pascal" inline>end</syntaxhighlight>]] usually is not necessary, but optional and it does not harm insert one anyway.
  
The semicolon [[ASCII]] or the unicode value is 59 ([[Hexadecimal]] $3B).
+
As a demonstration, that a single semicolon can make the difference, consider the following listings:
 +
<syntaxhighlight lang="pascal" highlight="2">
 +
case c of
 +
0: if false then c := 42;
 +
else c := -1;
 +
end;
 +
</syntaxhighlight>
 +
If <syntaxhighlight lang="pascal" inline>c</syntaxhighlight> is zero, it remains zero, but becomes <syntaxhighlight lang="pascal" inline>-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" inline>c</syntaxhighlight> only becomes <syntaxhighlight lang="pascal" inline>-1</syntaxhighlight> if it has 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" inline>begin</syntaxhighlight>]] and <syntaxhighlight lang="pascal" inline>end</syntaxhighlight>) where it is allowed, in order to mitigate such issues.
 +
{{Note|Using <syntaxhighlight lang="pascal" inline>otherwise</syntaxhighlight> (an [[Extended Pascal]] extension) instead of  <syntaxhighlight lang="delphi" inline>else</syntaxhighlight> in <syntaxhighlight lang="pascal" inline>case</syntaxhighlight>-statements may prevent such mistakes.}}
  
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.
+
=== empty statement ===
 +
In a sequence a semicolon without a preceding (qualified) statement indicates an '''empty statement'''.
  
(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.)
+
Historically empty statements were used in conjunction with [[Label|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.
  
The semicolon is generally mandatory, and is required after every statement.  The places where it is optional are
+
Also historically, [[Case|<syntaxhighlight lang="pascal" inline>case</syntaxhighlight>-statements]] had to list all possible values the selector variable theoretically could have.
* before an [[End|END]] statement
+
Now, if a value or range did not imply any action, yet had to be listed inside the <syntaxhighlight lang="pascal" inline>case</syntaxhighlight>-statement in order to fulfill this requirement, an empty statement is the shortest possible way to implement the situation.
* before the period at the end of a [[Program|PROGRAM]] or [[Unit|UNIT]]
 
  
The places where a semicolon is forbidden are
+
== other remarks ==
* Before a [[Begin|BEGIN]] statement which begins a compound statement
+
In the [[ASCII]] character set the semicolon takes the value <syntaxhighlight lang="pascal" inline>59</syntaxhighlight> ([[Hexadecimal|hexadecimal]] <syntaxhighlight lang="delphi" inline>$3B</syntaxhighlight>).
* Before an immediately following [[Else|ELSE]] clause of a nested IF statement
 
 
 
It is generally forbidden before the ELSE clause in an [[If|IF]] statement or a [[Case|CASE]] statement.  However, in the case of a nested IF statement it may be required before the ELSE, if there is an ELSE applying to another IF statement earlier in the program text and the particular IF is not ended by an ELSE statement.  This will be explained below.
 
 
 
== If statement and semicolon ==
 
 
 
A semicolon is forbidden before the ELSE clause which applies to the immediately last IF statement in order that the compiler can know where that IF statement ends.  For example
 
 
 
<source>
 
 
 
(*0001*)  if continuance then                   
 
(*0002*)    if separated then                 
 
(*0003*)      Condition(a)                     
 
(*0004*)    else                               
 
(*0005*)      if combined then               
 
(*0006*)          Condition(b)
 
(*0007*)      else                                         
 
(*0008*)          Condition(c);
 
(*0009*)  Calculate_fee;
 
 
 
</source>
 
 
 
For this example, the semicolon is forbidden on line 3 (otherwise the ELSE on line 4 would apply to the IF on line 1).
 
 
 
If there was going to be a BEGIN between lines 1 and 2, and an END after line 8, then a semicolon would be forbidden on line 1.
 
 
 
Because line 6 has no semicolon, the ELSE on line 7 applies to the IF on line 5.  If line 6 ended with a semi colon, then the ELSE on line 7 would apply to the IF on line 1 (the ELSE on line 4 applies to the IF on line 2).  Thus the semicolon is forbidden in this context in order to get that result.
 
 
 
However, if you ''wanted'' the ELSE to apply to line 1, the semicolon would be ''mandatory'' on line 6, in order to "close" the IF on line 5.
 
 
 
If there was an IF statement above line 1, the semicolon on line 8 closes the IF statement on line 1, and if an ELSE were to be placed after line 8, it would not apply to the IF statement on line 1.
 
 
 
It may be preferable to enclose all statements inside an IF statement with a BEGIN / END block if there might be a question as to which ELSE applies to which IF, in a set of nested IF statements.
 
  
 
{{Symbols}}
 
{{Symbols}}

Latest revision as of 21:56, 4 July 2021

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

;

The semicolon ; is used to

  • conclude a declaration,
  • conclude a constant, resourceString, or type definition,
  • separate formal parameters in a routine signature,
  • separate a routine declaration from its attributes,
  • terminate the program header,
  • separate alternatives in variant records, and to
  • separate statements, in contrast to other programming language where its purpose is to terminate a statement.

statement separator

necessity

Since language constructs only in their entirety constitute statements, semicolons may not split their components. Most notably ; cannot appear immediately before an else that is part of an if  then branch. However, ; in front of an end usually is not necessary, but optional and it does not harm insert one anyway.

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 if it has 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 is allowed, in order to mitigate such issues.

Note-icon.png

Note: Using otherwise (an Extended Pascal extension) instead of else in case-statements may prevent such mistakes.

empty statement

In a sequence a semicolon without a preceding (qualified) statement indicates an empty statement.

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.

other remarks

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


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)