HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to PascalVariablesData Types & ConversionOperatorsStringsConditions (if)Loops (for / while)Functions
REFERENCE
📖 Overview
🖥️ Output
📦 Variables
✍️ String
🔁 Condition & Loop
🧩 Function & Procedure
Pascal›Variables

Variables

integer, real, string, boolean types; var block and const constants.

Simple enough for a 5-year-old to understand

Typed variables

Example code
1
program H;
2
var
3
age: integer;
Integer.
VariableInteger.
4
height: real;
Decimal.
VariableDecimal.
5
name: string;
Text.
VariableText.
6
begin
7
age := 36;
8
name := 'Alex';
9
writeln(name, ' ', age);
Alex 36
Output / InputAlex 36
10
end.
VariableOutput / Input
💡 Hint: All variables must be declared in the var block before begin.

🎯 Exercise & Practice

Test what you've learned — answer, write code, check instantly. (1 left)

1.Define city (string), assign 'Trabzon' and print it.

🎯 Expected output: Trabzon

Loading editor…

About this topic

In Pascal, every variable must be declared before it is used. Declarations are gathered in a special section at the beginning of the program, before the body. This is the most visible part of the language's structural discipline.

The assignment operator consists of a colon and an equals sign. Comparison, on the other hand, is done with a single equals sign. In many languages these two can be confused; Pascal separates them, making accidental assignment errors impossible.

Every variable's type is written in its declaration and does not change. There is no type inference; this requires extra typing, but the person reading the code never has to guess anything.

What you will learn

  • The variable declaration section
  • The difference between assignment and comparison
  • The necessity of type declarations
  • Defining constants
  • Naming conventions

Who is this for?

  • Beginners to Pascal
  • Those not used to the requirement of declarations
  • Those confusing the assignment operator

When to use?

Values that will not change throughout the program should be defined in the constants section; this way they cannot be accidentally modified and gain a meaningful name in the code.

Keeping the declaration section organized makes it possible to see at a glance what data is being used in growing programs.

Common mistakes

Wrong

program Test;
begin
  sayi := 10;
  writeln(sayi);
end.

Since the variable is used without being declared, the compiler gives an "Identifier not found" error. Declaration is mandatory in Pascal and is done in the section preceding the body. This rule prevents typos from silently creating new variables.

Correct

program Test;
var
  sayi: Integer;
begin
  sayi := 10;
  writeln(sayi);
end.

Wrong

program Test;
var
  sayi: Integer;
begin
  sayi = 10;
  writeln(sayi);
end.

A single equals sign is the comparison operator; for assignment, a colon and an equals sign are used together. This code gives a syntax error. Pascal's distinction prevents the accidental assignment bug upfront, which runs silently in the C family and is hard to find.

Correct

program Test;
var
  sayi: Integer;
begin
  sayi := 10;
  writeln(sayi);
end.

Wrong

program Test;
var
  sayi: Integer;
begin
  sayi := 3.5;
  writeln(sayi);
end.

A decimal value cannot be assigned to an integer variable; the compiler gives a type mismatch error. Pascal doesn't truncate silently, it expects you to explicitly state what you want: rounding or truncating?

Correct

program Test;
var
  sayi: Integer;
begin
  sayi := Round(3.5);
  writeln(sayi);
end.

Frequently asked questions

Can I declare multiple variables on a single line?

Yes, you can define variables of the same type by separating them with commas in a single type declaration. Separate lines are required for different types. Doing logical grouping in the declaration section improves readability.

How is a constant defined?

In the constants section, which precedes the declaration section, the name and value are written; the type is usually inferred from the value. Constants cannot be changed throughout the program and are the best way to clean magic numbers out of the code.

Can I use Turkish characters in variable names?

It varies by compiler and is generally not recommended. Names must start with a letter and contain letters, numbers, and underscores. Using English and ASCII names in the source code is safe for portability.

Summary

Every variable is declared before it is used.

Declarations are in the section before the body.

Assignment is done with a colon and an equals sign.

Comparison is done with a single equals sign.

Type mismatches are caught at compile time.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code