Else

From Lazarus wiki

Deutsch (de) English (en) español (es) français (fr) русский (ru)

else is a keyword 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:

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.

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.

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.

if itIsMorning() then
	if itIsAHoliday() then
	begin
		sleep;
	end
	else
	begin
		wakeUp;
		dress;
		brushTeeth;
		;
	end
else
begin
	;
end;
Note-icon.png

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


Keywords: begindoelseendforifrepeatthenuntilwhile