Difference between revisions of "Else"

From Lazarus wiki
Jump to navigationJump to search
m (→‎see also: update Reference Guide subsection link number)
 
(2 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
{{else}}
 
{{else}}
  
<syntaxhighlight lang="pascal" enclose="none">else</syntaxhighlight> is a [[Keyword|keyword]] which starts a fallback-branch if all other ''named'' cases do not apply.
+
<syntaxhighlight lang="pascal" inline>else</syntaxhighlight> is a [[Reserved word|reserved word]] which starts a fallback-branch if all other ''named'' cases do not apply.
 
It can occur in
 
It can occur in
* [[If and Then|<syntaxhighlight lang="pascal" enclose="none">if … then … else</syntaxhighlight>-branches]], and
+
* [[If and Then|<syntaxhighlight lang="pascal" inline>if … then … else</syntaxhighlight>-branches]], and
* [[Case#case-statements|<syntaxhighlight lang="pascal" enclose="none">case</syntaxhighlight>-statements]].
+
* [[Case#case-statements|<syntaxhighlight lang="pascal" inline>case</syntaxhighlight>-statements]].
  
 
== semantics ==
 
== semantics ==
An <syntaxhighlight lang="pascal" enclose="none">else</syntaxhighlight>-branch obtains program flow, if no other condition has been met.
+
An <syntaxhighlight lang="pascal" inline>else</syntaxhighlight>-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 <syntaxhighlight lang="pascal" enclose="none">else</syntaxhighlight> per se does not have a condition.
+
It cannot be paired with an explicit [[expression]], but depends on expressions stated at others place, so an <syntaxhighlight lang="pascal" inline>else</syntaxhighlight> per se does not have a condition.
In <syntaxhighlight lang="pascal" enclose="none">if … then … else</syntaxhighlight>-statements, instructions are executed to the following scheme:
+
In <syntaxhighlight lang="pascal" inline>if … then … else</syntaxhighlight>-statements, instructions are executed to the following scheme:
 
<syntaxhighlight lang="pascal">if expression
 
<syntaxhighlight lang="pascal">if expression
 
then trueStatement
 
then trueStatement
 
else falseStatement;
 
else falseStatement;
</syntaxhighlight>Where <syntaxhighlight lang="pascal" enclose="none">falseStatement</syntaxhighlight> is executed if <syntaxhighlight lang="pascal" enclose="none">expression</syntaxhighlight> evaluates to [[false and true|<syntaxhighlight lang="pascal" enclose="none">false</syntaxhighlight>]].
+
</syntaxhighlight>Where <syntaxhighlight lang="pascal" inline>falseStatement</syntaxhighlight> is executed if <syntaxhighlight lang="pascal" inline>expression</syntaxhighlight> evaluates to [[false and true|<syntaxhighlight lang="pascal" inline>false</syntaxhighlight>]].
  
In <syntaxhighlight lang="pascal" enclose="none">case</syntaxhighlight>-statements an <syntaxhighlight lang="pascal" enclose="none">else</syntaxhighlight>-branch assumes program flow, if no <syntaxhighlight lang="pascal" enclose="none">case</syntaxhighlight>-labels matched <syntaxhighlight lang="pascal" enclose="none">expression</syntaxhighlight>.
+
In <syntaxhighlight lang="pascal" inline>case</syntaxhighlight>-statements an <syntaxhighlight lang="pascal" inline>else</syntaxhighlight>-branch assumes program flow, if no <syntaxhighlight lang="pascal" inline>case</syntaxhighlight>-labels matched <syntaxhighlight lang="pascal" inline>expression</syntaxhighlight>.
 
<syntaxhighlight lang="pascal">case expression of
 
<syntaxhighlight lang="pascal">case expression of
 
value0: action0;
 
value0: action0;
 
value1: action1;
 
value1: action1;
 
else action2;
 
else action2;
end;</syntaxhighlight>Only if <syntaxhighlight lang="pascal" enclose="none">expression</syntaxhighlight> neither evaluates to <syntaxhighlight lang="pascal" enclose="none">value0</syntaxhighlight> nor <syntaxhighlight lang="pascal" enclose="none">value1</syntaxhighlight>, <syntaxhighlight lang="pascal" enclose="none">action2</syntaxhighlight> is executed.
+
end;</syntaxhighlight>Only if <syntaxhighlight lang="pascal" inline>expression</syntaxhighlight> neither evaluates to <syntaxhighlight lang="pascal" inline>value0</syntaxhighlight> nor <syntaxhighlight lang="pascal" inline>value1</syntaxhighlight>, <syntaxhighlight lang="pascal" inline>action2</syntaxhighlight> is executed.
  
 
=== comparative remarks ===
 
=== comparative remarks ===
In Pascal there is no <syntaxhighlight lang="perl" enclose="none">elsif</syntaxhighlight> or <syntaxhighlight lang="bash" enclose="none">elif</syntaxhighlight>.
+
In [[Pascal]] there is no <syntaxhighlight lang="perl" inline>elsif</syntaxhighlight> or <syntaxhighlight lang="bash" inline>elif</syntaxhighlight>.
However writing <syntaxhighlight lang="pascal" enclose="none">else</syntaxhighlight> and <syntaxhighlight lang="pascal" enclose="none">if</syntaxhighlight> back to back does not pose a problem.
+
However writing <syntaxhighlight lang="pascal" inline>else</syntaxhighlight> and <syntaxhighlight lang="pascal" inline>if</syntaxhighlight> back to back does not pose a problem.
Note, that the second <syntaxhighlight lang="pascal" enclose="none">if … then</syntaxhighlight> constitutes on its own a single statement.
+
Note, that the second <syntaxhighlight lang="pascal" inline>if … then</syntaxhighlight> constitutes on its own a single [[statement]].
The requirement that <syntaxhighlight lang="pascal" enclose="none">else</syntaxhighlight> is followed by a statement is therefore fulfilled.
+
The requirement that <syntaxhighlight lang="pascal" inline>else</syntaxhighlight> is followed by a statement is therefore fulfilled.
 
<syntaxhighlight lang="pascal">if expression0 then
 
<syntaxhighlight lang="pascal">if expression0 then
 
begin
 
begin
Line 36: Line 36:
 
end;</syntaxhighlight>
 
end;</syntaxhighlight>
  
=== nested <syntaxhighlight lang="pascal" enclose="none">if … then … else</syntaxhighlight> ===
+
=== nested <syntaxhighlight lang="pascal" inline>if … then … else</syntaxhighlight> ===
<syntaxhighlight lang="pascal" enclose="none">if … then … else</syntaxhighlight> are prone to semantic errors if no compound statements by enclosing a block with [[Begin|<syntaxhighlight lang="pascal" enclose="none">begin</syntaxhighlight>]] and [[End|<syntaxhighlight lang="pascal" enclose="none">end</syntaxhighlight>]] are used.
+
<syntaxhighlight lang="pascal" inline>if … then … else</syntaxhighlight> are prone to semantic errors if no compound statements by enclosing a block with [[Begin|<syntaxhighlight lang="pascal" inline>begin</syntaxhighlight>]] and [[End|<syntaxhighlight lang="pascal" inline>end</syntaxhighlight>]] are used.
 
<syntaxhighlight lang="pascal">if itIsMorning() then
 
<syntaxhighlight lang="pascal">if itIsMorning() then
 
if itIsAHoliday() then
 
if itIsAHoliday() then
Line 54: Line 54:
 
…;
 
…;
 
end;</syntaxhighlight>
 
end;</syntaxhighlight>
{{Note|In front of an <syntaxhighlight lang="pascal" enclose="none">else</syntaxhighlight> as part of an <syntaxhighlight lang="pascal" enclose="none">if … then … else</syntaxhighlight>-statement no [[;|semicolon]] is permitted.}}
+
{{Note|In front of an <syntaxhighlight lang="pascal" inline>else</syntaxhighlight> as part of an <syntaxhighlight lang="pascal" inline>if … then … else</syntaxhighlight>-statement no [[;|semicolon]] is permitted.}}
 
The reference guide explains, quote:
 
The reference guide explains, quote:
 
<blockquote>
 
<blockquote>
In nested <syntaxhighlight lang="pascal" enclose="none">If.. then .. else</syntaxhighlight> constructs, some ambiguity may [arise] as to which <syntaxhighlight lang="pascal" enclose="none">else</syntaxhighlight> statement pairs with which <syntaxhighlight lang="pascal" enclose="none">if</syntaxhighlight> statement. The rule is that the <syntaxhighlight lang="pascal" enclose="none">else</syntaxhighlight> keyword matches the first <syntaxhighlight lang="pascal" enclose="none">if</syntaxhighlight> keyword (searching backwards) not already matched by an <syntaxhighlight lang="pascal" enclose="none">else</syntaxhighlight> keyword.
+
In nested <syntaxhighlight lang="pascal" inline>If.. then .. else</syntaxhighlight> constructs, some ambiguity may [arise] as to which <syntaxhighlight lang="pascal" inline>else</syntaxhighlight> statement pairs with which <syntaxhighlight lang="pascal" inline>if</syntaxhighlight> statement. The rule is that the <syntaxhighlight lang="pascal" inline>else</syntaxhighlight> [[Keyword|keyword]] matches the first <syntaxhighlight lang="pascal" inline>if</syntaxhighlight> keyword (searching backwards) not already matched by an <syntaxhighlight lang="pascal" inline>else</syntaxhighlight> keyword.
 
</blockquote>
 
</blockquote>
  
 
== see also ==
 
== see also ==
* [https://www.freepascal.org/docs-html/ref/refsu57.html “The <syntaxhighlight lang="pascal" enclose="none">If..then..else</syntaxhighlight> statement in “Free Pascal reference guide”]
+
* [https://www.freepascal.org/docs-html/ref/refsu56.html “The <syntaxhighlight lang="pascal" inline>If..then..else</syntaxhighlight> statement in “Free Pascal reference guide”]
  
 
{{Keywords}}
 
{{Keywords}}
[[category:Pascal]]
 
[[Category:Control Structures]]
 

Latest revision as of 22:27, 10 December 2020

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

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:

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;
Light bulb  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