Difference between revisions of ";"

From Free Pascal wiki
Jump to navigationJump to search
(Remove everything that states a semicolon can be immediately before an else statement that belongs to an if statement. Add example of semicolon before else in a case statement)
Line 2: Line 2:
  
  
<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>
+
<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 [[ASCII]] or the unicode value is 59 ([[Hexadecimal]] $3B).
Line 9: Line 9:
  
 
(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.)
 
(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.)
 +
<br><br><br><br><br><br>
  
 
The semicolon is generally mandatory, and is required after every statement.  The places where it is optional are
 
The semicolon is generally mandatory, and is required after every statement.  The places where it is optional are
* before an [[End|END]] statement
+
* before an [[End|END]] statement  
* before the period at the end of a [[Program|PROGRAM]] or [[Unit|UNIT]]
+
<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 places where a semicolon is forbidden are
+
The else part belongs to the case statement.<br>
* Before a [[Begin|BEGIN]] statement which begins a compound statement  
+
DoNotA will be executed if none of the case labels (just one in this case) applies.
* Before an immediately following [[Else|ELSE]] clause of a nested IF statement
+
<br>
 +
<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.
  
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}}

Revision as of 14:59, 2 April 2018

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


;

The semicolon ASCII or the unicode value is 59 (Hexadecimal $3B).

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.

(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.)





The semicolon is generally mandatory, and is required after every statement. The places where it is optional are

  • before an END statement


A semicolon is forbidden

  • immediately before an ELSE statement that belongs to an if statement


Notice that a semicolon can occur immediately before an else statement that belongs to a case statement.
Consider the following:

  case a of
    true: if b then DoB;
    else DoNotA;
  end;

The else part belongs to the case statement.
DoNotA will be executed if none of the case labels (just one in this case) applies.

Now if you leave out the semicolon after DoB

  case a of
    true: if b then DoB
    else DoNotB;
  end;

The else statement now is part of the "if b".
DoNotB will only be executed if a is true, and b is false.



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)