Syntaxübersicht
| Konzept | Pascal |
|---|---|
| Programm | program Name; ... begin ... end. |
| Variable | var x: Integer; |
| Konstante | const Max = 10; |
| Zuweisung | x := 5; |
| Bedingung | if a then b else c; |
| Schleife | for i := 1 to 10 do, while, repeat..until |
| Funktion | function F(x: Integer): Integer; |
| Prozedur | procedure P(var a: Integer); |
| Record | type TP = record ... end; |
| Klasse | type TK = class ... end; |
| Zeiger | ^Integer, New, Dispose, nil |
| Kommentar | { ... }, (* ... *), // Zeile |
Nützliche Routinen
program Referenz;
{$mode objfpc}{$H+}
uses SysUtils, StrUtils, Math;
var
a: array[1..3] of Integer = (3, 1, 2);
i: Integer;
begin
writeln(Odd(3), ' ', Ord('a'), ' ', Chr(97), ' ', Abs(-2), ' ', Sqr(4));
writeln(Trunc(2.7), ' ', Round(2.5), ' ', Frac(2.75):0:2, ' ', Int(2.75):0:1);
writeln(Length('Pascal'), ' ', Copy('Pascal', 1, 3), ' ', Pos('s', 'Pascal'));
writeln(UpperCase('x'), LowerCase('Y'), ' ', IntToStr(5), ' ', BoolToStr(True, True));
writeln(IfThen(3 > 2, 'ja', 'nein'), ' ', Sign(-9), ' ', Random(1) = 0);
for i := Low(a) to High(a) do write(a[i], ';');
writeln;
writeln(SizeOf(Integer), ' ', SizeOf(Double), ' ', SizeOf(Char));
writeln(MaxInt, ' ', High(Int64));
end.Ausgabe
TRUE 97 a 2 16 2 2 0.75 2.0 6 Pas 3 Xy 5 True ja -1 TRUE 3;1;2; 4 8 1 2147483647 9223372036854775807
Typische Fehler
program F;
var x: Integer;
begin
x := 'text';
end.Fehlermeldung
Incompatible types
program G;
begin
writeln(unbekannt);
end.Fehlermeldung
Identifier not found "unbekannt"
Weiterführende Quellen
- freepascal.org: Compiler und Dokumentation (englisch)
- lazarus-ide.org: IDE, Wiki und Foren
- „Pascal Programming“-Handbuch im Free-Pascal-Wiki (wiki.freepascal.org)
- „Programming in Pascal“ (Wirth) als klassische Lektüre
Merke
- Pascal ist streng strukturiert, gut lesbar und für Lernende hervorragend geeignet
- Object Pascal bringt Klassen, Generics und Interfaces, Lazarus/Delphi eine komfortable IDE
Aufgabe
Portiere ein kleines Programm aus einer anderen Sprache nach Pascal.