next up previous contents index search.gif
Next: 7.2 Structured statements Up: 7. Statements Previous: 7. Statements

Subsections


7.1 Simple statements

A simple statement cannot be decomposed in separate statements. There are basically 4 kinds of simple statements:

Simple statements

\begin{syntdiag}\setlength {\sdmidskip}{.5em}\sffamily\sloppy \synt{siple\ state...
...tatement} \\
\synt{goto\ statement}\\
\synt{raise\ statement}
\)\end{syntdiag}
Of these statements, the raise statement will be explained in the chapter on Exceptions (chapter Exceptions)

7.1.1 Assignments

Assignments give a value to a variable, replacing any previous value the observable might have had:

Assignments

\begin{syntdiag}\setlength {\sdmidskip}{.5em}\sffamily\sloppy \synt{assignment\ ...
...{+=} \\
\lit*{-=} \\
\lit*{*=} \\
\lit*{/=}
\)\synt{expression}\end{syntdiag}
In addition to the standard Pascal assignment operator ( := ), which simply replaces the value of the varable with the value resulting from the expression on the right of the := operator, Free Pascal supports some c-style constructions. All available constructs are listed in table (assignments) .

Table: Allowed C constructs in Free Pascal
Assignment Result
a += b Adds b to a, and stores the result in a.
a -= b Substracts b from a, and stores the result in a.
a *= b Multiplies a with b, and stores the result in a.
a /= b Divides a through b, and stores the result in a.

For these constructs to work, you should specify the -Sc command-line switch. Remark: These constructions are just for typing convenience, they don't generate different code. Here are some examples of valid assignment statements:

X := X+Y;
X+=Y;      { Same as X := X+Y, needs -Sc command line switch}
X/=2;      { Same as X := X/2, needs -Sc command line switch}
Done := False;
Weather := Good;
MyPi := 4* Tan(1);

7.1.2 Procedure statements

Procedure statements are calls to subroutines. There are different possibilities for procedure calls: A normal procedure call, an object method call (qualified or not) , or even a call to a procedural type variable. All types are present in the following diagram.

Procedure statements

\begin{syntdiag}\setlength {\sdmidskip}{.5em}\sffamily\sloppy \synt{procedure\ s...
...egin{displaymath}
\synt{actual\ parameter\ list}
\end{displaymath}\end{syntdiag}
The Free Pascal compiler will look for a procedure with the same name as given in the procedure statement, and with a declared parameter list that matches the actual parameter list. The following are valid procedure statements:

Usage;
WriteLn('Pascal is an easy language !');
Doit();

7.1.3 Goto statements

Free Pascal supports the goto jump statement. Its prototype syntax is

Goto statement

\begin{syntdiag}\setlength {\sdmidskip}{.5em}\sffamily\sloppy \synt{goto\ statement} \lit*{goto} \synt{label}\end{syntdiag}
When using goto statements, you must keep the following in mind:
  1. The jump label must be defined in the same block as the Goto statement.
  2. Jumping from outside a loop to the inside of a loop or vice versa can have strange effects.
  3. To be able to use the Goto statement, you need to specify the -Sg compiler switch.
Goto statements are considered bad practice and should be avoided as much as possible. It is always possible to replace a goto statement by a construction that doesn't need a goto, although this construction may not be as clear as a goto statement. For instance, the following is an allowed goto statement:

label
  jumpto;
...
Jumpto :
  Statement;
...
Goto jumpto;
...


root
1999-06-10