DMarlon Studio

Lesson 2 · 2 of 20

Variables and basic types · Pascal

Declare variables in Pascal's var block with the basic types (Integer, Double, Char, Boolean and String) and give them values with :=.

Pascal · Variables and basic types in DMarlon Studio: the example's code and its output in the terminal

In Pascal every variable is declared before it is used, in the `var` block, with its name and its type: shirts: Integer;. The compiler checks types, so it will not let you store text in a number variable.

The basic types are `Integer` for whole numbers, `Double` (or `Real`) for numbers with decimals, `Char` for a single character in quotes ('M'), `Boolean` for True or False, and `String` for text. Case does not matter: WriteLn and writeln are the same.

Values are assigned with `:=`; a plain = is for comparing in Pascal. WriteLn prints a Boolean as TRUE and a real number in scientific notation: 1.2500000000000000E+001 is 12.5. The :0:2 in shirts * price:0:2 prints it with two decimals; lesson 4 covers it properly.

A variable can change its value but not its type: shirts := shirts + 2 adds the two T-shirts that arrive. `High(Integer)` gives the largest number an Integer can hold: 2147483647.

Code · main.pas

program main;// Every variable is declared in the var block, with its type.var  shop: String;  shirts: Integer;  price: Double;  size: Char;  inStock: Boolean;begin  // := gives a variable its value.  shop := 'Corner Shop';  shirts := 3;  price := 12.5;  size := 'M';  inStock := True;  WriteLn('Shop: ', shop);  WriteLn('T-shirts: ', shirts, ' in size ', size);  WriteLn('In stock? ', inStock);  WriteLn('Price: ', price);  // A variable can change, but always keeps its type.  shirts := shirts + 2;  WriteLn('T-shirts after the delivery: ', shirts);  WriteLn('Value of the stock: ', shirts * price:0:2);  WriteLn('Largest Integer: ', High(Integer));end.

Output · fpc main.pas && ./main

Shop: Corner Shop
T-shirts: 3 in size M
In stock? TRUE
Price:  1.2500000000000000E+001
T-shirts after the delivery: 5
Value of the stock: 62.50
Largest Integer: 2147483647

Key points

  • Variables are declared in var as name: Type;.
  • := assigns; = compares.
  • Integer, Double, Char, Boolean and String are the basic types.
  • A variable can change its value, never its type.

Exercise

Add a discount: Double variable set to 0.10 and print the price of one T-shirt with the discount applied (price * (1 - discount)), with two decimals.