Statements
Tusk mirrors Delphi's statement syntax very closely,
but this article briefly summarizes the different
kinds of statements for easy reference.
Tusk uses the := operator for
assignment statements.
The left-hand side must be one of the following…
- a variable: x, value, etc.
- a dot expression: p.x, Employee.LastName, etc.
- a subscript expression: a[i], List.Values[Index], etc.
- a dereference expression: p^, PtrArray[k]^
Tusk uses the if, then, and else
keywords for the basic
conditional statement.
Examples…
if a < b then
Writeln('Too small');
if IsSpecial(k) then begin
Inc(SpecialCount);
Writeln('special');
end else
Writeln('boring');
Tusk uses the case keyword to support
the switch statement.
The following details are important…
- The expression must be an integer, enum, or character;
- Tusk does not use fall-through;
- Tusk supports multiple values (using ,);
- Tusk supports ranges of values (using ..);
- Tusk offers a novel strict option for case statements.
Tusk supports while loops
with the while keyword.
Examples…
while a < b do
Inc(a);
while not i.EOF do begin
Writeln(i.Value);
i.Next;
end;
Tusk uses the repeat and until keywords
to support the
do-while loop.
Example…
repeat
Success := TryProcess(Item);
Writeln(Success);
until not Success;
Tusk uses the for, to, downto,
and do keywords to support
for loops.
The following details are important…
- The loop variable must be an integer, enum, or character;
- The start and stop values are evaluated once, before executing the first iteration of the loop;
- The loop variable is incremented or decremented by one each iteration;
- The loop variable may not be modified within the loop;
- A new loop variable may be declared using the var keyword (the variable is in scope only within the loop).
Tusk uses the for, in, and do
keywords to support
for-each loops.
The following details are important…
- The loop variable may not be modified within the loop;
- The container expression may be: a string, an AnsiString, a set, an array, or a suitable interface or record;
- A new loop variable may be declared using the var keyword (the variable is in scope only within the loop).
Tusk uses the Break keyword to
terminate a loop early.
Examples…
for i := 1 to 100 do begin
Writeln(i);
if NotValid(i) then
Break;
end;
while a < b do begin
a := f(a);
b := f(b);
if b < 0 then
Break;
end;
Tusk uses the Continue keyword to
jump to the next loop iteration.
Example…
for Reader in ReadOnlyDB.RunReader(Query) do begin
if ShouldSkip(Reader['customer_id']) then
Continue;
Writeln('Date: ', Reader['trd_date'], '; Qty: ', Reader['qty']);
ProcessTrade(Reader);
end;
Tusk uses the Exit keyword to implement the
return statment.
Examples…
procedure Work;
begin
for var i := 1 to 10 do begin
if f(i) < 0 then
Exit;
Writeln(i);
end;
Writeln('complete');
end;
function GetDescr(i: Integer): string;
begin
if i < 0 then
Exit('Negative');
Result := IntToStr(i);
if i < 100 then
Exit;
Result := Result + ' (large value)');
end;
As demonstrated above, Exit can be used without
an argument (in procedures and functions)
or with an argument (in functions only).
Tusk uses the try, finally,
and except keywords to implement
exception handling.
Examples…
Setup;
try
Work1;
Work2;
Work3;
finally
Cleanup;
end;
try
Work;
except
Writeln('Work failed');
raise;
end;
Tusk uses the raise keyword to raise
exceptions.
Examples…
try
Work;
except
Writeln('Work failed');
raise;
end;
Tusk uses the begin and end keywords for
compound statements.
⏱ Last Modified: 2/15 10:06:11 am