Sign in Join
Wiki copytemporary

Else

Deutsch · English · Español · Français · Suomi · Русский

else is a reserved word which starts a fallback-branch if all other named cases do not apply. It can occur in

semantics

An else-branch obtains program flow, if no other condition has been met. It cannot be paired with an explicit expression, but depends on expressions stated at others place, so an else per se does not have a condition. In if … then … else-statements, instructions are executed to the following scheme:

pascal
if expression
    then trueStatement
    else falseStatement;

Where falseStatement is executed if expression evaluates to false.

In case-statements an else-branch assumes program flow, if no case-labels matched expression.

pascal
case expression of
    value0: action0;
    value1: action1;
    else action2;
end;

Only if expression neither evaluates to value0 nor value1, action2 is executed.

comparative remarks

In Pascal there is no elsif or elif. However writing else and if back to back does not pose a problem. Note, that the second if … then constitutes on its own a single statement. The requirement that else is followed by a statement is therefore fulfilled.

pascal
if expression0 then
begin
    action0;
end
else if expression1 then
begin
    action1;
end;

nested if … then … else

if … then … else are prone to semantic errors if no compound statements by enclosing a block with begin and end are used.

pascal
if itIsMorning() then
    if itIsAHoliday() then
    begin
        sleep;
    end
    else
    begin
        wakeUp;
        dress;
        brushTeeth;
        ???;
    end
else
begin
    ???;
end;

Note: In front of an else as part of an if … then … else-statement no semicolon is permitted.

The reference guide explains, quote:

In nested If.. then .. else constructs, some ambiguity may [arise] as to which else statement pairs with which if statement. The rule is that the else keyword matches the first if keyword (searching backwards) not already matched by an else keyword.

see also


From the Free Pascal wiki, page "Else", as it stood on 2020-12-10 after a change by Kai Burghardt, out of the wiki's dump of 2024-05-26. 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 1, last changed by WikiArchive .

Talk about this page

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