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›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
var score: integer;
3
begin
4
score := 75;
5
if score >= 50 then
6
writeln('Passed')
NO ; (since else follows).
Output / InputNO ; (since else follows).
7
else
8
writeln('Failed');
9
end.
Output / Input
💡 Hint: if ... then LINE1 (without ;) else LINE2; — no ; before else.

About this topic

Conditions in Pascal are written with if, then, and else. If you want to bind multiple statements to a condition, you need to wrap them with block keywords; indentation on its own has no meaning.

Pascal's most famous trap is the semicolon before else. Because semicolons separate statements, placing one immediately before else causes the if statement to be considered finished right there, and the compiler flags else as misplaced.

For multi-way decisions, the case structure is used. It branches based on constant values and ranges, offers much better readability than long conditional chains, and its branches do not automatically fall through to the next.

What you will learn

  • The if, then, and else structure
  • Block usage
  • The rule about the semicolon before else
  • the case structure
  • Nested conditions

Who is this for?

  • Students learning Pascal
  • Those encountering the else error
  • Developers coming from the C family

When to use?

An if chain is sufficient for two or three-state decisions; for multi-way decisions based on constant values, case is more readable.

If multiple statements are to be bound to a condition, using a block is mandatory; forgetting it leads to a silent logic error.

Common mistakes

Wrong

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

The semicolon before else terminates the if statement; the compiler finds the following else unexpected and throws an error. In Pascal, a semicolon is a separator, not a terminator. This is the language's most common error.

Correct

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

Wrong

program Test;
var
  a: Integer;
begin
  a := 5;
  if a > 3 then
    writeln('greater');
    writeln('this is thought to be conditional');
end.

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

Correct

program Test;
var
  a: Integer;
begin
  a := 5;
  if a > 3 then
  begin
    writeln('greater');
    writeln('both are conditional');
  end;
end.

Wrong

program Test;
var
  a: Integer;
begin
  a := 5;
  if a = 5 then;
    writeln('equal');
end.

A semicolon after then creates an empty statement; the condition does nothing and the print runs in all cases. The compiler does not throw an error, making this very hard to find. Checking the position of the semicolon is critical.

Correct

program Test;
var
  a: Integer;
begin
  a := 5;
  if a = 5 then
    writeln('equal');
end.

Frequently asked questions

How do I remember the semicolon rule?

Semicolons separate statements. Therefore, they are placed between two statements; they are not placed in the middle of a structure, for example between then and else. Writing it after the last statement in a block is harmless but unnecessary.

What does case match with?

With countable types: integers, characters, and countable type values. Ranges and comma-separated lists can also be written. It does not match with strings; a conditional chain is required for that.

Is there a default branch in case?

Yes, a section that runs if no branch matches can be defined. If it is omitted and no branch matches, the structure silently does nothing; adding this section to catch unexpected values is a good habit.

Summary

A semicolon is never placed before else.

A semicolon after then produces an empty statement.

A block is required for multiple statements.

Indentation has no meaning.

case branches based on constant values.

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