100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Programming

Your First Pascal Program

Write, compile, and run a simple Pascal program that reads user input and prints output, tying together everything from earlier topics.

FoundationsBeginner8 min readJul 10, 2026
Analogies

Your First Pascal Program

The traditional first program in any language simply prints a greeting to the screen, and in Pascal that means using the WriteLn procedure inside a minimal program skeleton: a heading, an empty declaration section, and a main begin...end block. WriteLn appends a newline after its output automatically, while its sibling Write does not, and both accept a comma-separated list of values, strings, numbers, or expressions, that get concatenated in the printed output without any manual string formatting required.

🏏

Cricket analogy: WriteLn automatically starting a new line after each call is like a scorer automatically starting a fresh line in the scorebook after each over, you don't have to manually insert the line break yourself.

Compiling and Running the Program

Once the source file is saved with a .pas extension, for example 'greet.pas', you compile it from the terminal with 'fpc greet.pas', which produces an executable (greet on Linux/macOS, greet.exe on Windows) alongside object files used internally by the compiler. Running that executable directly, './greet' or 'greet.exe', executes your compiled machine code immediately, no separate interpreter step is involved the way there is with Python, because Pascal compiles all the way down to native code ahead of time.

🏏

Cricket analogy: Compiling greet.pas into a standalone executable is like a player completing all their training drills and finally being match-ready, once compiled, the program runs directly without needing the 'coach' (compiler) present anymore.

Reading User Input

To make a program interactive, Pascal provides ReadLn, which pauses execution, waits for the user to type a line of text and press Enter, and stores that value into the variable you pass it, automatically converting the typed text into the variable's declared type. If you call ReadLn(age) where age is an Integer, and the user types something that isn't a valid whole number, the program will raise a runtime error, which is why well-written Pascal programs often validate input or wrap risky reads in error handling before trusting the value.

🏏

Cricket analogy: ReadLn pausing execution until the user responds is like a bowler waiting at the top of their mark for the umpire's signal before starting their run-up, nothing proceeds until that input arrives.

pascal
program Greet;
var
  userName: String;
  age: Integer;
begin
  Write('What is your name? ');
  ReadLn(userName);

  Write('How old are you? ');
  ReadLn(age);

  WriteLn('Hello, ', userName, '!');
  WriteLn('In 10 years you will be ', age + 10, ' years old.');
end.

Write and WriteLn accept optional field-width and decimal-place formatting, for example WriteLn(price:8:2) prints 'price' right-aligned in an 8-character field with exactly 2 decimal places, which is the standard way to format currency-like output in Pascal without a separate formatting library.

Calling ReadLn(age) when age is declared as Integer and the user types non-numeric text (like 'twenty') will cause a runtime error and abruptly terminate the program unless you've wrapped the read in exception handling. Beginners often assume Pascal 'gracefully' rejects bad input the way some form-validation libraries do; it does not, by default.

  • A minimal Pascal program is a heading followed by an empty declaration section and a begin...end block.
  • WriteLn prints values and appends a newline; Write prints without one.
  • Source files are compiled with 'fpc filename.pas', producing a native executable ahead of time.
  • The compiled executable runs directly with no separate interpreter step needed at runtime.
  • ReadLn pauses execution and converts typed input into the target variable's declared type.
  • Passing bad input to ReadLn for a numeric variable causes a runtime error, not a graceful rejection.
  • Field-width and decimal formatting, like WriteLn(x:8:2), control output alignment without extra libraries.

Practice what you learned

Was this page helpful?

Topics covered

#Programming#PascalStudyNotes#YourFirstPascalProgram#Pascal#Program#Compiling#Running#StudyNotes#SkillVeris#ExamPrep