Skip to content
Tutorials Center
My learning

Topics

Lesson 3 of 36 ยท Getting Started

Your First Pascal Program

Write, read and understand your first Pascal programs: Hello World, Write versus WriteLn, escaping quotes, formatting numbers and reading input with ReadLn.

Every language course starts with a program that prints a greeting, and Pascal's is pleasantly short. In this lesson you will write it, take it apart line by line, then learn the output routines you will use constantly: Write, WriteLn, formatted numbers and reading keyboard input with ReadLn.

Pascal listings here are read-only. Run them in Free Pascal, Lazarus or a Delphi console project, as described in Setting Up Free Pascal, Lazarus and Delphi. Runnable demos on this page are C stand-ins and say so.

Hello, world

PASCAL
program Hello;

begin
  WriteLn('Hello, world!');
end.

Output:

TEXT
Hello, world!

Reading it line by line:

  1. program Hello; names the program. In Free Pascal this line is optional, but including it is a good habit. The name must be an identifier (letters, digits, underscores, not starting with a digit).
  2. begin opens the main block, the code that runs when the program starts.
  3. WriteLn('Hello, world!'); calls a built-in procedure that prints its argument and a newline. Text is enclosed in single quotes.
  4. end. closes the main block. The full stop tells the compiler the program is finished; anything after it is ignored.

Write and WriteLn

Write prints without moving to a new line. WriteLn prints and then starts a new line. Both accept several arguments separated by commas, of mixed types:

PASCAL
program Mixed;

begin
  Write('Score: ');
  Write(42);
  WriteLn;
  WriteLn('Pi is about ', 3.14159:0:2);
  WriteLn('Yes? ', True);
end.

Output:

TEXT
Score: 42
Pi is about 3.14
Yes? TRUE

A call with no arguments, like WriteLn;, just ends the current line. Notice 3.14159:0:2: after a number you may add :width:decimals. A width of 0 means "as wide as needed", and 2 means two digits after the decimal point. Without it, a real number prints in scientific notation, which is rarely what you want. Booleans print as TRUE or FALSE.

Quotes and special characters

Single quotes delimit strings. To put an apostrophe inside, double it:

PASCAL
program Quotes;

begin
  WriteLn('It''s a nice day');
  WriteLn('Tab:', #9, 'after');
  WriteLn('Smile: ', #65, #66, #67);
end.

Output:

TEXT
It's a nice day
Tab:	after
Smile: ABC

The notation #9 means "the character with code 9" (a tab), and #65 is A. You can join text and codes directly, as in 'Line one'#13#10'Line two', although WriteLn is simpler for line breaks.

Reading input

ReadLn waits for the user to type a line and press Enter, then stores the value in a variable. Variables must be declared first, in a var section before begin:

PASCAL
program Greet;

var
  name: string;
  age: Integer;

begin
  Write('Your name: ');
  ReadLn(name);
  Write('Your age: ');
  ReadLn(age);
  WriteLn('Hello ', name, ', next year you will be ', age + 1, '.');
end.

If the user types Mia and 30, the program prints Hello Mia, next year you will be 31.. We cover declarations properly in Variables, Constants and Assignment. Two details to remember:

  • Read(x) reads a value and leaves the cursor on the same line; ReadLn(x) also discards the rest of the line. When mixing them with text prompts, prefer ReadLn.
  • If the user types something that is not a number into an Integer variable, a run-time error occurs. You will learn to guard against that with exceptions later in the course.

Keeping the window open

When you run a console program by double-clicking it on Windows, the window closes the moment the program ends. A common trick while learning is a final prompt:

PASCAL
program Pause;

begin
  WriteLn('Done.');
  Write('Press Enter to exit...');
  ReadLn;
end.

A typical Delphi version

Delphi's console template adds a few lines, but the heart is the same:

PASCAL
program Project1;

{$APPTYPE CONSOLE}

uses
  System.SysUtils;

begin
  try
    WriteLn('Hello from Delphi');
  except
    on E: Exception do
      WriteLn(E.ClassName, ': ', E.Message);
  end;
end.

{$APPTYPE CONSOLE} tells the compiler to build a console program, uses imports a unit and try ... except catches errors. Each piece is explained later; for now, know that the part you write goes between begin and end..

Stand-in: formatted output

Pascal's :0:2 formatting corresponds to %.2f in C. Here is a C stand-in that prints the same kinds of values, including a "Pascal-style" booleans TRUE/FALSE helper. It is C code, to run on this site.

C C stand-in: formatted output

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

C C stand-in: reading two numbers

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the difference between Write and WriteLn?

    Both print their arguments, but WriteLn ends the line afterwards. WriteLn with no arguments just prints a newline.
  2. 2. How do you put an apostrophe inside a Pascal string?

    In Pascal two single quotes inside a string stand for one apostrophe. There are no backslash escapes.
  3. 3. What does WriteLn(2.5:0:1) control?

    The :width:decimals suffix formats a real number. Without it the value prints in scientific notation.
  4. 4. What must the last line of a Pascal program be?

    The main block closes with end. including the full stop, which marks the end of the program.

Summary

  • A program is program Name; then begin ... end.; statements end with semicolons.
  • Write prints, WriteLn prints and ends the line; reals use :width:decimals.
  • Strings use single quotes; double a quote to include one; #n inserts a character code.
  • ReadLn(variable) reads input into a declared variable.
  • Delphi console projects add {$APPTYPE CONSOLE}, uses and a try ... except wrapper.

Next: Program Structure, Comments and Style