13.1.1 Assignments

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

_________________________________________________________________________________________________________
Assignments

--assignment- statement-|-variable- identifier----:= ----expression ----------
                    |-function- identifier-| -+=  -|
                                      |--= --|
                                      | *=  -|
                                      - /=  --
____________________________________________

In addition to the standard Pascal assignment operator (:=), which simply replaces the value of the variable 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 (13.1).


Table 13.1: Allowed C constructs in Free Pascal

Assignment Result


a += b Adds b to a, and stores the result in a.
a -= b Subtracts 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 (only for floats, not integers).



For C-like assignments, the $COPERATORS directive must be enabled or the -Sc command-line switch must be specified when compiling. These constructions are just for typing convenience and generate the same code.

Remark
Here is an example with some valid assignment statements:

var
  Done: Boolean;
  S: String;
  X, Y: Double;
begin
  Done:=False or (1=2);
  S:='text';
  X:=X+Y;
  {$COPERATORS ON}
  X+=Y; // same as X:=X+Y
  X/=2; // same as X:=X/2
end.

Keeping in mind that the dereferencing of a typed pointer results in a variable of the type the pointer points to, the following are also valid assignments:

var
  L : ˆLongint;
  P : PPChar;
begin
  Lˆ:=3;
  Pˆˆ:='A';
end.

Note the double dereferencing in the second assignment.