Difference between revisions of "Else"

From Lazarus wiki
Jump to navigationJump to search
m (→‎see also: update Reference Guide subsection link number)
 
(10 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 
{{else}}
 
{{else}}
<br>
 
[http://www.freepascal.org/docs-html/ref/refsu51.html#x144-15400013.2.3 Else at Language Reference]
 
  
Else is [[Keyword|keyword]] which introduces the action to do if the condition is [[False|false]].
+
<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
 +
* [[If and Then|<syntaxhighlight lang="pascal" inline>if … then … else</syntaxhighlight>-branches]], and
 +
* [[Case#case-statements|<syntaxhighlight lang="pascal" inline>case</syntaxhighlight>-statements]].
  
= [[If]] [[Then|then]] else =
+
== semantics ==
<syntaxhighlight>
+
An <syntaxhighlight lang="pascal" inline>else</syntaxhighlight>-branch obtains program flow, if no other condition has been met.
  if (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.
  then true_statement
+
In <syntaxhighlight lang="pascal" inline>if … then else</syntaxhighlight>-statements, instructions are executed to the following scheme:
  else false_statement;
+
<syntaxhighlight lang="pascal">if expression
</syntaxhighlight>
+
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>]].
  
The value of ''condition'' is evaluated, if it resolves to true, the ''true_statement'' is executed, otherwise the ''false_statement'' is executed.  The value of condition ''must'' resolve to a boolean value or an error occurs.
+
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.
  
=== More statements in "if then else" statement ===
+
=== 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>
  
If you need two or more statements for true_statement or false_statement, then the group of statements must be placed within a [[Begin|begin]] ... [[End|end]] [[Block]].
+
=== 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;
 +
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>
  
<syntaxhighlight>
+
== see also ==
  if boolean_condition then
+
* [https://www.freepascal.org/docs-html/ref/refsu56.html “The <syntaxhighlight lang="pascal" inline>If..then..else</syntaxhighlight> statement in “Free Pascal reference guide”]
    begin
 
      statement_one;
 
      statement_two;
 
    end
 
  else
 
    begin
 
      statement_three;
 
      statement_four;
 
    end;
 
</syntaxhighlight>
 
 
 
In regular usage, the "else" statement is a special exception to the rule that every statement is followed by a semicolon. Neither the "else" keyword nor the statement immediately preceding it may have a semicolon following it. In the above example the first "end" statement is not followed by a semicolon but the last one is. 
 
 
 
However, in the case of nested "if" statements, if the ''else'' applies to the "inner" if, then the semicolon must ''not'' appear before the ''else'', if the ''else'' applies to the outer else, then a semicolon ''must'' appear before it:
 
 
 
<syntaxhighlight>
 
  if a then
 
      if b then
 
        begin
 
          (..)
 
        end;
 
      else
 
        begin
 
          (..)
 
        end;
 
</syntaxhighlight>
 
In this case, the "else" applies to "if a"
 
<syntaxhighlight>
 
  if a then
 
      if b then
 
        begin
 
          (..)
 
        end
 
      else
 
        begin
 
          (..)
 
        end;
 
</syntaxhighlight>
 
In this case, the "else" applies to "if b".  If this causes ambiguity, it can be resolved by coding an "empty" else statement:
 
<syntaxhighlight>
 
  if a then
 
      if b then
 
        begin
 
          (..)
 
        end
 
      else
 
  else
 
      begin
 
          (..)
 
      end
 
</syntaxhighlight>
 
  
 
{{Keywords}}
 
{{Keywords}}
[[category:Pascal]]
 
[[Category:Control Structures]]
 

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