typestar

Variables and constants in Delphi

A bakery till works out a morning's takings from a fixed loaf price.

program VariablesConst;
uses SysUtils;

const
  ShopName = 'Rye and Reason';
  LoafPrice = 4.25;
var
  LoavesSold: Integer;
  Takings: Double;
  DoorOpen: Boolean;
begin
  // := assigns to a variable; = only names a constant or compares.
  LoavesSold := 38;
  Takings := LoavesSold * LoafPrice;
  DoorOpen := False;
  WriteLn(ShopName);
  WriteLn(Format('%d loaves sold, %.2f taken', [LoavesSold, Takings]));
  WriteLn('door open: ', DoorOpen);
end.

How it works

  1. The const block names ShopName and LoafPrice, neither of which can change.
  2. The var block declares an Integer, a Double and a Boolean in one place.
  3. Takings := LoavesSold * LoafPrice; assigns with :=, never with =.

Keywords and builtins used here

The run, in numbers

Lines
19
Characters to type
425
Tokens
79
Three-star pace
80 tpm

At the three-star pace of 80 tokens a minute, this run takes about 59 seconds.

Type this snippet

Step 2 of 3 in Programs & variables, step 2 of 27 in Language basics.

← Previous Next →

Variables and constants in other languages