typestar

Assignment and arithmetic in Delphi

A bus conductor tracks riders boarding and splits a fare into dollars and cents.

program AssignmentOps;
uses SysUtils;

var
  Riders, Boarding, Fare, Dollars, Cents: Integer;
begin
  Riders := 12;                 // := stores a value
  Boarding := 5;
  Riders := Riders + Boarding;  // Delphi has no += operator
  Inc(Riders, 3);               // Inc adds, Dec subtracts
  Dec(Riders);
  Fare := 275;
  Dollars := Fare div 100;      // div is integer division
  Cents := Fare mod 100;        // mod is the leftover remainder
  WriteLn(Format('riders on board: %d', [Riders]));
  WriteLn(Format('fare of %d cents is %d dollars %d cents',
    [Fare, Dollars, Cents]));
end.

How it works

  1. Riders := Riders + Boarding; stands in for the += operator Delphi does not have.
  2. Inc(Riders, 3) adds three and Dec(Riders) takes one back off.
  3. Fare div 100 is integer division and Fare mod 100 is the remainder left over.

Keywords and builtins used here

The run, in numbers

Lines
18
Characters to type
564
Tokens
103
Three-star pace
80 tpm

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

Type this snippet

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

← Previous Next →