ZetCode

Pascal conditionals

last modified November 18, 2026 Free Pascal 3.2.2

A conditional chooses which statements to run. Pascal has two forms: the if statement, which tests a Boolean expression, and the case statement, which selects one branch from a list of constant labels. This article shows both, including the dangling else, the semicolon trap, and the Free Pascal extension that allows strings in a case.

If and then

The simplest conditional is if condition then statement. The condition must be a Boolean expression, and the statement after then runs only when it is true. A compound statement between begin and end may take the place of the single statement.

ifthen.pas
program ifthen;
{$mode objfpc}{$H+}

var
  temperature: Integer;

begin
  temperature := 30;
  if temperature > 25 then
    WriteLn('It is hot.');
  WriteLn('Done.');
end.

Only the WriteLn('It is hot.') call belongs to the if; the following statement runs either way. The indentation shows the intended grouping but does not affect the meaning, because Pascal uses begin and end to group statements, not layout.

$ fpc -gl ifthen.pas
$ ./ifthen
It is hot.
Done.

If, then, and else

An else branch runs when the condition is false. There is no then after else, and the statement before else does not take a semicolon. The two branches must produce compatible statements; in practice either both are single statements or both are compound statements between begin and end.

ifelse.pas
program ifelse;
{$mode objfpc}{$H+}

var
  number: Integer;

begin
  number := 7;
  if number mod 2 = 0 then
    WriteLn(number, ' is even')
  else
    WriteLn(number, ' is odd');
end.

number mod 2 is zero for an even number and one for an odd one, and the comparison produces the Boolean that if expects. The program prints one line, not two, because exactly one branch runs. The else branch can be omitted when there is nothing to do, as in the previous section.

$ fpc -gl ifelse.pas
$ ./ifelse
7 is odd

Else-if chains

Pascal has no else if keyword; an else-if chain is written by placing another if statement after else. The indentation is only a convention. The chain is tested from top to bottom, and the first true condition wins, so the order of the tests matters.

elseif.pas
program elseif;
{$mode objfpc}{$H+}

var
  score: Integer;

begin
  score := 82;
  if score >= 90 then
    WriteLn('A')
  else if score >= 80 then
    WriteLn('B')
  else if score >= 70 then
    WriteLn('C')
  else
    WriteLn('F');
end.

A score of 82 is not at least 90, but it is at least 80, so the second branch prints B and the remaining tests are skipped. The final else belongs to the last if. If the tests were written in the opposite order, a score of 82 would match the first test and print the wrong grade.

$ fpc -gl elseif.pas
$ ./elseif
B

The dangling else

An else always belongs to the nearest preceding if that does not already have one. In a nested condition this can be different from what the indentation suggests. The next program places an else under the outer if, but the compiler attaches it to the inner one.

dangling.pas
program dangling;
{$mode objfpc}{$H+}

var
  a, b: Boolean;

begin
  a := False;
  b := True;
  WriteLn('start');
  if a then
    if b then
      WriteLn('both')
    else
      WriteLn('not both');
  WriteLn('end');
end.

The outer condition is false, so the whole inner if is skipped and neither message prints. The else belongs to the inner if, not to the outer one. To attach it to the outer if, put the inner statement between begin and end, or add an explicit else to the inner one.

$ fpc -gl dangling.pas
$ ./dangling
start
end

The semicolon trap

A semicolon before else is a common mistake. The semicolon ends the if statement, so the else that follows has no matching if and the compiler reports a syntax error. The same rule explains why the statement before else must not end with a semicolon when it is a single statement.

semielse.pas
program semielse;
{$mode objfpc}{$H+}

var
  n: Integer;

begin
  n := 1;
  if n = 1 then
    WriteLn('one');
  else
    WriteLn('other');
end.

The compiler stops at the else and reports that a semicolon was expected, which is its way of saying that the if statement was already complete. Removing the semicolon after WriteLn('one') fixes the program. A compound branch can end with a semicolon before end, because the semicolon then belongs inside the block.

$ fpc semielse.pas
semielse.pas(9,3) Fatal: Syntax error, ";" expected but "ELSE" found
Fatal: Compilation aborted

The case statement

A case statement selects one branch by comparing an ordinal expression with constant labels. A label may be a single value, a comma-separated list, or a range written with two dots. The optional else branch catches every value that no label matches. The else part is a Free Pascal extension; ISO Pascal has no catch-all label in a case statement.

caseint.pas
program caseint;
{$mode objfpc}{$H+}

var
  day: Integer;

begin
  day := 3;
  case day of
    1, 7: WriteLn('weekend');
    2..6: WriteLn('weekday');
  else
    WriteLn('invalid day');
  end;
end.

The value 3 matches the range 2..6, so the program prints weekday. The labels must be constants and must not overlap; the compiler rejects a duplicate label. A case statement does not fall through: after a branch runs, control continues after end. There is no break between branches.

$ fpc -gl caseint.pas
$ ./caseint
weekday

Case with characters and strings

The selector of a case can be any ordinal type, so characters work as well as integers. Ranges of characters are written in the same way. Free Pascal also accepts strings as labels, which is useful for command parsing but is not portable to every Pascal compiler.

casechar.pas
program casechar;
{$mode objfpc}{$H+}

var
  c: Char;

begin
  c := 'g';
  case c of
    'a'..'z': WriteLn('lower case letter');
    'A'..'Z': WriteLn('upper case letter');
    '0'..'9': WriteLn('digit');
  else
    WriteLn('other');
  end;
end.

The character g falls in the first range, so the program prints lower case letter. The ranges depend on the character set; on ASCII and UTF-8 the letters are contiguous, which is what makes this style work. A character outside all three ranges reaches the else branch.

$ fpc -gl casechar.pas
$ ./casechar
lower case letter

Case with strings

String labels are compared case-sensitively and exactly, without trimming blanks. The next program dispatches on a command name and prints a message for each known value. An unknown command reaches the else branch.

casestring.pas
program casestring;
{$mode objfpc}{$H+}

var
  command: string;

begin
  command := 'start';
  case command of
    'start': WriteLn('starting');
    'stop':  WriteLn('stopping');
    'help':  WriteLn('showing help');
  else
    WriteLn('unknown command');
  end;
end.

The program prints starting. The labels are string literals, so 'Start' would not match 'start'. For a case-insensitive command parser, convert the input with LowerCase first, or use an if-chain with SameText. Later articles in the series show both approaches.

$ fpc -gl casestring.pas
$ ./casestring
starting

Nested conditions

Conditions can be nested, and a compound statement can hold several statements in either branch. The next program tests two values and prints one of three messages. It uses begin and end around the outer true branch so that the inner if is unambiguous.

casenested.pas
program casenested;
{$mode objfpc}{$H+}

var
  x, y: Integer;

begin
  x := 5;
  y := 0;
  if x > 0 then
  begin
    if y = 0 then
      WriteLn('x is positive, y is zero')
    else
      WriteLn('x is positive, y is not zero');
  end
  else
    WriteLn('x is not positive');
end.

The outer if checks x, and the inner one checks y only when x is positive. The else after end belongs to the outer if. Inside a procedure, Exit leaves the procedure immediately and can replace a deeply nested chain; the procedures article shows that form.

$ fpc -gl casenested.pas
$ ./casenested
x is positive, y is zero

Boolean conditions

A condition does not need an explicit comparison with True. A Boolean variable or function result is already a condition, so if isReady then is preferred to if isReady = True then. When two comparisons are joined, each must be in parentheses because and binds tighter than the comparison operators, as the operators article explains.

A condition can also call a function, as long as the function returns a Boolean. This is common for validation routines such as IsValid(input). The function is evaluated once per test, and short-circuit evaluation means that later tests in an and expression are skipped when an earlier one fails.

Next steps

The loops article repeats statements with for, while, and repeat, and uses the same Boolean conditions to decide when to stop. The procedures article shows how to package a condition in a function and how Exit leaves a procedure early.

Source

Free Pascal Reference guide

Free Pascal: control statements

Pascal on Wikipedia

In this article we have used if, else, else-if chains, the case statement with integers, ranges, characters, and strings, and looked at the dangling else and the semicolon trap. The series continues with Pascal loops.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all Pascal tutorials.