HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to DelphiVariablesData Types & ConversionOperatorsStringsConditions (if)Loops (for / while)Functions
REFERENCE
📖 Overview
🖥️ Output
📦 Variables
🔄 Data Types & Conversion
✍️ String
🔁 Condition & Loop
🧩 Function & Procedure
Delphi›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
{$MODE DELPHI}
3
var
var block.
Variablevar block.
4
age: Integer;
Integer.
VariableInteger.
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 Delphi, every variable is declared and its type is fixed. Declarations are gathered in a block before the body; in modern versions, inline local declarations are also possible, but the classical format is still widespread.

Assignment is done with a colon and an equals sign; a single equals sign is for comparison. This distinction, inherited from Pascal, makes accidental assignment errors catchable at compile time.

In Delphi, when variables are not initialized, the behavior depends on the type. Global variables are zeroed out, but local variables contain garbage data; this leads to hard-to-find bugs, and explicitly giving every local variable an initial value is a good habit.

What you will learn

  • The variable declaration section
  • Distinguishing assignment from comparison
  • Defining constants
  • The difference between local and global variables
  • The risk of uninitialized variables

Who is this for?

  • Beginners to Delphi
  • Those transitioning from Pascal
  • Those dealing with unexpected values

When to use?

Values that do not change throughout the program should be defined as constants; the compiler embeds them directly into the code and they cannot be accidentally modified.

Local variables are preferred because their scope is small; global variables make it hard to track who modified what and when in growing projects.

Common mistakes

Wrong

var
  toplam: Integer;
  i: Integer;
begin
  for i := 1 to 5 do
    toplam := toplam + i;
  writeln(toplam);
end.

Since the Total variable is not initialized, it starts with garbage data in the local context, and the result can be different every time it runs. At best, the compiler gives a warning. Accumulator variables must always be explicitly zeroed out.

Correct

var
  toplam: Integer;
  i: Integer;
begin
  toplam := 0;
  for i := 1 to 5 do
    toplam := toplam + i;
  writeln(toplam);
end.

Wrong

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 written together. This code will not compile. Delphi's distinction prevents silent bugs upfront where assignment and comparison are confused.

Correct

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

Wrong

const
  MaksAdet = 10;
begin
  MaksAdet := 20;
  writeln(MaksAdet);
end.

Constants cannot be changed; attempting to assign to them results in a compilation error. If a value needs to change at runtime, you must define it as a variable. Constants are meant for naming magic numbers in code.

Correct

var
  maksAdet: Integer;
begin
  maksAdet := 10;
  maksAdet := 20;
  writeln(maksAdet);
end.

Frequently asked questions

Can I declare a variable where I use it?

In Delphi 10.3 and later, inline declarations are supported and type inference is also possible. If you need to stay compatible with older versions, you must use the classical declaration section.

Why aren't local variables zeroed out?

Because zeroing is costly and Delphi prefers performance. Managed types (strings, interfaces, dynamic arrays) are automatically cleaned up, but the responsibility is yours for numbers and pointers.

Is it correct to declare multiple variables on a single line?

Variables of the same type can be declared on a single line separated by commas. For readability, it is good to group related variables, but breaking them into meaningful lines rather than long lists is clearer.

Summary

Every variable is declared and its type is fixed.

Assignment is done with a colon and an equals sign.

Local numeric variables do not initialize themselves to zero.

Accumulators must be given an explicit initial value.

Constants cannot be changed at runtime.

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