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
Riders := Riders + Boarding;stands in for the+=operator Delphi does not have.Inc(Riders, 3)adds three andDec(Riders)takes one back off.Fare div 100is integer division andFare mod 100is the remainder left over.
Keywords and builtins used here
DecFormatIncIntegerWriteLnbegindivendmodprogramusesvar
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.
Step 3 of 3 in Programs & variables, step 3 of 27 in Language basics.