🏛️ Pascal (Delphi / Free Pascal)

Структурный язык: типы, циклы, процедуры, записи, строки.

Шпаргалки · Программирование · #pascal #delphi #freepascal

IDE и запуск

Lazarus (Free Pascal, бесплатно), Delphi (Embarcadero), PascalABC.NET (учебный). fpc program.pas. Режим Delphi: {$mode delphi}.

Структура программы

program Demo;
var
  n: Integer;
begin
  n := 5;
  WriteLn('n = ', n);
end.

Присваивание :=, сравнение =, конец программы end.

Типы данных

Тип Пример
Integer, Int64, Byte 42
Real, Double, Extended 3.14
Boolean True
Char, String 'a', 'text'
Массив, запись, множество array[1..5] of Integer, record, set of Byte
Указатель ^Integer
type
  TUser = record
    Id: Integer;
    Name: string;
  end;
var a: array[1..3] of Integer = (1, 2, 3);

Операторы

+ - * /, div, mod, = <> < >, and or not xor, shl shr, in, @ (адрес).

Циклы и условия

for i := 1 to 10 do WriteLn(i);
for i := 10 downto 1 do ;
while n > 0 do n := n - 1;
repeat n := n + 1 until n = 5;
if n > 0 then WriteLn('+') else WriteLn('-');
case n of 1: WriteLn('один'); 2..5: WriteLn('мало'); else WriteLn('иначе'); end;

Процедуры и функции

procedure Hello(const name: string);
begin WriteLn('Привет, ', name); end;

function Add(a, b: Integer): Integer;
begin Result := a + b; end;

var перед параметром передача по ссылке.

Встроенные

WriteLn, ReadLn, Length, Copy, Pos, UpperCase, IntToStr, StrToInt, Abs, Sqr, Sqrt, Random, Inc, Dec, Ord, Chr.

Асинхронность

TThread (Delphi / Lazarus), TTask (Delphi PPL), Application.ProcessMessages.

Коллекции и библиотеки

TList<T>, TDictionary<K,V>, TStringList (RTL). Компоненты: VCL, FireMonkey (Delphi), LCL (Lazarus), Indy (сеть), FireDAC (БД), DUnit / DUnitX (тесты).