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
- The
constblock namesShopNameandLoafPrice, neither of which can change. - The
varblock declares anInteger, aDoubleand aBooleanin one place. Takings := LoavesSold * LoafPrice;assigns with:=, never with=.
Keywords and builtins used here
BooleanDoubleFalseFormatIntegerWriteLnbeginconstendprogramusesvar
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.
Step 2 of 3 in Programs & variables, step 2 of 27 in Language basics.