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›Conditions (if)

Conditions (if)

if-then-else; no semicolon before else, and begin..end blocks.

Simple enough for a 5-year-old to understand

if / then / else

WARNING: Do NOT put a semicolon BEFORE else.

Example code
1
program H;
2
{$MODE DELPHI}
3
var score: Integer;
4
begin
5
score := 75;
6
if score >= 50 then
7
writeln('Passed')
NO ; (since else follows).
Output / InputNO ; (since else follows).
8
else
9
writeln('Failed');
10
end.
Output / Input
💡 Hint: if ... then LINE1 (without ;) else LINE2; — no ; before else.

About this topic

Conditions in Delphi use the same structure as Pascal: if, then, and else. Block keywords are required to bind multiple statements to a condition, and indentation holds no meaning.

The rule of not placing a semicolon before else applies here as well, and it is the most common compilation error in Delphi. Semicolons separate statements; they do not go in the middle of structures.

For multi-branch decisions, there is the case structure. It works with countable types and does not match against text. If branching based on text is needed, you must write a chain of conditions or convert the text to a countable value.

What you will learn

  • The if, then, and else structure
  • Block usage
  • The rule about the semicolon before else
  • the case structure and its limits
  • simplifying nested conditions

Who is this for?

  • developers learning Delphi
  • Those encountering the else error
  • those writing multi-branch decisions

When to use?

For decisions with a few states, a condition chain is more readable; for multi-branch decisions based on fixed values, case is more readable.

The early exit pattern — catching and exiting invalid states at the beginning — is the most effective way to prevent deep nested conditions.

Common mistakes

Wrong

var
  a: Integer;
begin
  a := 5;
  if a > 3 then
    writeln('greater');
  else
    writeln('smaller');
end.

The semicolon before else terminates the if statement, and the compiler finds the following else unexpected. The semicolon is a separator; it is not placed between the then branch and else. This is a first-day error encountered in Delphi.

Correct

var
  a: Integer;
begin
  a := 5;
  if a > 3 then
    writeln('greater')
  else
    writeln('smaller');
end.

Wrong

var
  a: Integer;
begin
  a := 5;
  if a > 3 then
    writeln('greater');
    writeln('this is thought to be dependent on the condition');
end.

Only the first statement is bound to the condition; the second line runs in all cases even if indented. Delphi ignores indentation. Block keywords must be used to bind multiple statements.

Correct

var
  a: Integer;
begin
  a := 5;
  if a > 3 then
  begin
    writeln('greater');
    writeln('both are dependent on the condition');
  end;
end.

Wrong

var
  durum: string;
begin
  durum := 'aktif';
  case durum of
    'aktif': writeln('running');
    'pasif': writeln('stopped');
  end;
end.

The case structure only works with countable types; matching with text is not supported and results in a compilation error. To branch based on text, you need to write a condition chain or convert the text to a countable type.

Correct

var
  durum: string;
begin
  durum := 'aktif';
  if durum = 'aktif' then
    writeln('running')
  else if durum = 'pasif' then
    writeln('stopped');
end.

Frequently asked questions

Is there a ternary operator?

Not in the language, but the standard library contains functions that return one of two values based on a condition. Note: since this is a function, both values are evaluated; do not use it with expressions that have side effects.

can I write ranges in the case structure?

Yes, you can write ranges with two dots and comma-separated lists. A section that will run when no branch matches can also be defined; adding this to catch unexpected values is a good habit.

How can I simplify nested conditions?

Catching invalid states at the beginning of the function and exiting immediately is the most effective way. Thus, the main business logic remains in a flat flow instead of nested blocks, and readability increases significantly.

Summary

A semicolon is never placed before else.

A block is required for multiple statements.

Indentation has no meaning.

case does not match with text.

Early exit reduces nested conditions.

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