Difference between revisions of "Else"
m |
m (→see also: update Reference Guide subsection link number) |
||
(14 intermediate revisions by 7 users not shown) | |||
Line 1: | Line 1: | ||
− | + | {{else}} | |
− | = [[ | + | <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. |
− | <syntaxhighlight> | + | It can occur in |
− | + | * [[If and Then|<syntaxhighlight lang="pascal" inline>if … then … else</syntaxhighlight>-branches]], and | |
− | + | * [[Case#case-statements|<syntaxhighlight lang="pascal" inline>case</syntaxhighlight>-statements]]. | |
− | |||
− | </syntaxhighlight> | ||
− | + | == semantics == | |
+ | 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" inline>else</syntaxhighlight> per se does not have a condition. | ||
+ | In <syntaxhighlight lang="pascal" inline>if … then … else</syntaxhighlight>-statements, instructions are executed to the following scheme: | ||
+ | <syntaxhighlight lang="pascal">if expression | ||
+ | then trueStatement | ||
+ | else falseStatement; | ||
+ | </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" 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 | ||
+ | value0: action0; | ||
+ | value1: action1; | ||
+ | else action2; | ||
+ | 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 === | |
+ | In [[Pascal]] there is no <syntaxhighlight lang="perl" inline>elsif</syntaxhighlight> or <syntaxhighlight lang="bash" inline>elif</syntaxhighlight>. | ||
+ | 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" inline>if … then</syntaxhighlight> constitutes on its own a single [[statement]]. | ||
+ | The requirement that <syntaxhighlight lang="pascal" inline>else</syntaxhighlight> is followed by a statement is therefore fulfilled. | ||
+ | <syntaxhighlight lang="pascal">if expression0 then | ||
+ | begin | ||
+ | action0; | ||
+ | end | ||
+ | else if expression1 then | ||
+ | begin | ||
+ | action1; | ||
+ | end;</syntaxhighlight> | ||
− | <syntaxhighlight> | + | === nested <syntaxhighlight lang="pascal" inline>if … then … else</syntaxhighlight> === |
− | + | <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 | |
− | + | if itIsAHoliday() then | |
− | + | begin | |
− | + | sleep; | |
− | + | end | |
− | + | else | |
− | + | begin | |
− | + | wakeUp; | |
− | + | dress; | |
− | </syntaxhighlight> | + | brushTeeth; |
+ | …; | ||
+ | end | ||
+ | else | ||
+ | begin | ||
+ | …; | ||
+ | end;</syntaxhighlight> | ||
+ | {{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: | ||
+ | <blockquote> | ||
+ | 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> | ||
− | + | == see also == | |
− | + | * [https://www.freepascal.org/docs-html/ref/refsu56.html “The <syntaxhighlight lang="pascal" inline>If..then..else</syntaxhighlight> statement in “Free Pascal reference guide”] | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | < | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | </syntaxhighlight> | ||
{{Keywords}} | {{Keywords}} | ||
− |
Latest revision as of 23: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;
The reference guide explains, quote:
In nested
If.. then .. else
constructs, some ambiguity may [arise] as to whichelse
statement pairs with whichif
statement. The rule is that theelse
keyword matches the firstif
keyword (searching backwards) not already matched by anelse
keyword.
see also
Keywords: begin — do — else — end — for — if — repeat — then — until — while