Sign in Join
Wiki copytemporary

Basic Pascal Tutorial/Chapter 3/IF

English · Français · Български · 中文(中国) · 日本語

◄ ▲ ►

Back to Reserved words.

3Ca - IF (author: Tao Yue, state: changed)

The IF statement allows you to branch based on the result of a Boolean operation.

Format

IF expression THEN

statement1

[ ELSE

statement2]

Where

expression is any comparison, constant or function which returns a boolean value, and

statement1 and statement2 are either a single statement, a Begin-end block, a repeat-until block, or the null statement.

The ELSE clause is optional.

There are two ways to use an IF statement, a one-way branch or a two-way branch/

One-way branch

The one-way branch format is:

pascal
if BooleanExpression then
      StatementIfTrue;

If the Boolean expression evaluates to true, the statement executes. Otherwise, it is skipped.

The IF statement accepts only one statement. If you would like to use a compound statement, you must use a begin-end frame to enclose the statements:

pascal
if BooleanExpression then
    begin
      Statement1;
      Statement2;
    end;

Two-way branch

There is also a two-way selection:

pascal
if BooleanExpression then
      StatementIfTrue
    else
      StatementIfFalse;

Note there is never a semicolon ; immediately before the else.

pascal
if BooleanExpression then
    begin
      Statement1;  // semicolon here is mandatory
      Statement2;  // semicolon here is optional
    end    // semicolon here is forbidden
    else
    begin
      Statement3;
      Statement4;
    end;

If the Boolean expression evaluates to FALSE, the statement following the else will be performed. Note that you may never use a semicolon after the statement preceding the else. That causes the computer to treat it as a one-way selection, leaving it to wonder where the else came from. And when a compiler wonders, it usually gets mad and throws a tantrum, or rather, it throws an error

If you need multi-way selection, simply nest if statements:

pascal
if Condition1 then
      Statement1
    else
      if Condition2 then
        Statement2
      else
        Statement3;

Be careful with nesting. Sometimes the computer won't do what you want it to do:

pascal
if Condition1 then
      if Condition2 then
        Statement2
    else
      Statement1;

The else is always matched with the most recent if, so the computer interprets the preceding block of code as:

pascal
if Condition1 then
      if Condition2 then
        Statement2
      else
        Statement1;

You can get by with a null statement:

pascal
if Condition1 then
      if Condition2 then
        Statement2
      else
    else
      Statement1;

Or you could use a begin-end block.

The following proves a semicolon is absolutely forbidden before an else:

pascal
// Paul Robinson 2020-12-16
    
    // Compiler test program  Err03.pas
    // tests the proposition that ; is
    // never legal before ELSE
    
    
    program Err03;
    Var
        Test,Test2: Boolean;
    
    
    Begin
    
        Test := True;
        Test2 := True;
    
        if Test then
           if Test2 then
               Writeln('Reached Part 1');  // semi-colon here should be illegal
         else
            Writeln('Reached Part 2');
    
    end.

But the best way to clean up the code would be to rewrite the condition.

pascal
if not Condition1 then
      Statement1
    else
      if Condition2 then
        Statement2;

This example illustrates where the not operator comes in very handy. If Condition1 had been a Boolean like: (not(a < b) or (c + 3 > 6)) and g, reversing the expression would be more difficult than NOTting it.

Also notice how important indentation is to convey the logic of program code to a human, but the compiler ignores the indentation.

◄ ▲ ►

From the Free Pascal wiki, page "Basic Pascal Tutorial/Chapter 3/IF", as the Internet Archive kept it on 2025-04-01, last changed on 2025-03-01. Copyright stays with the author who wrote it, according to the old wiki's Licensing page. If you have concerns about this material, message the admin.

Revision 4, last changed by WikiArchive .

Talk about this page

Nobody has opened a bench for this page yet. Sign in to open one.