Statement separators
Statements are separated by semicolons, not ended by them: the semicolon is
written between two statements. A semicolon before end,
until, else is allowed, because it ends with an
empty statement. A semicolon directly before else is an error,
because it would end the if statement before its else.
Statements may have a label in front: Done: WriteLn('x');.
Assignment and calls
X := 3; { assignment }
A[I].Name := 'x'; { to a field of an element }
Count += 1; { with {$COPERATORS ON} (Free Pascal): += -= *= /= }
Result := Left + Right; { the value a function returns }
Writeln('hello'); { a procedure call }
Obj.Method(1, 2); { a method call }
Add(5); { a function call whose result is dropped (extended syntax, {$X+}) }
The assignment operator is :=. The left side is a variable, a function
name (inside that function, in ISO 7185 and Turbo Pascal to give the result), or
Result. A procedure call is its name with arguments in parentheses; with no
parameters the parentheses are optional. A function may be called as a statement and its
result ignored when extended syntax is on (the default in Delphi and Free Pascal).
Compound statement
begin
A := 1;
B := 2
end
begin … end groups statements into one. It is the body of every
routine and the main program.
if
if X > 0 then
WriteLn('positive')
else if X < 0 then
WriteLn('negative')
else
WriteLn('zero');
The condition is a Boolean expression. There is no semicolon before else. In
a chain, an else belongs to the nearest if that has none (the
dangling else); use begin … end to change that.
case
case Ch of
'a'..'z': WriteLn('lower');
'A'..'Z': WriteLn('upper');
'0'..'9', '_': WriteLn('word');
else
WriteLn('other'); { Turbo Pascal, Delphi, Free Pascal; otherwise in ISO 10206 and Free Pascal }
end;
The selector is an ordinal expression. Each arm has one or more constants or ranges, separated by commas, then
a colon and a statement. The arms must not overlap. ISO 7185 has no else:
a selector value with no arm is an error. Turbo Pascal and later add else, and
Extended Pascal adds otherwise (Free Pascal accepts it too). A semicolon is
allowed before else in a case.
for
for I := 1 to 10 do Sum := Sum + I;
for I := 10 downto 1 do Write(I, ' ');
for C := 'a' to 'z' do Write(C);
for Colour := Red to Blue do ...
for Item in List do Process(Item); { Delphi and Free Pascal }
for Ch in 'text' do Write(Ch);
for var I := 0 to 9 do ... { Delphi 10.3 and later: the variable is declared here }
- The control variable is an ordinal variable that, in ISO 7185 and in Delphi, must be local to the routine. The limits are evaluated once, before the loop. The loop body must not change the control variable.
- With
tothe variable counts up, withdowntoit counts down. If the limits make the loop empty, the body does not run. After a normal end the value of the variable is undefined in ISO 7185 and most dialects. for … in(Delphi, Free Pascal) loops over the elements of an array, a set, a string, an enumeration, or an object that provides an enumerator: aGetEnumeratormethod returning an object withMoveNextandCurrent.
while and repeat
while not Eof(F) do
ReadLn(F, Line);
repeat
Read(Ch);
until Ch = #13;
while tests its condition before each pass, so the body may never run.
repeat … until runs the statements between the two words at
least once and stops when the condition becomes true; it needs no
begin and end to hold several statements.
with
with Rec do
begin
X := 1; { Rec.X }
Y := 2; { Rec.Y }
end;
with A, B do ... { the same as with A do with B do }
with makes the fields and methods of a record, object, class or interface
directly visible in the statement. A name in the body is looked for in the innermost
with first, which can hide a variable of the same name outside it.
goto
goto Label jumps to a statement that has that label in the same block (see
Declarations). Structured programming avoids
it, and it is mainly used to leave deeply nested loops.
exit, break, continue, halt
Exitleaves the current routine (or the main program or unit initialisation).Exit(Value)setsResultand leaves (Delphi 2009 and later, Free Pascal).Breakleaves the innermostfor,whileorrepeatloop.Continueskips to its next pass. Turbo Pascal 7, Delphi and Free Pascal have these two.HaltorHalt(Code)ends the program with an exit code.RunError(N)ends it with a run-time error.
try, raise and asm
try … except … end, try … finally … end and raise are described
on Exceptions and memory. An asm … end
block is a statement that holds assembly language, described on
Assembler.
Empty statement
A statement may be empty: if Done then ;, repeat until Done;,
begin end. An unintended empty statement after a stray semicolon, as in
for I := 1 to 10 do;, is a common mistake that makes the loop run an empty
body.