Difference between revisions of "Else"

From Lazarus wiki
Jump to navigationJump to search
m (→‎see also: update Reference Guide subsection link number)
 
(12 intermediate revisions by 5 users not shown)
Line 1: Line 1:
 
{{else}}
 
{{else}}
<br>
 
Else is [[Keyword|keyword]] which introduces the action to do if the condition is [[False|false]].
 
  
= [[If]] [[Then|then]] 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 (condition)
+
* [[If and Then|<syntaxhighlight lang="pascal" inline>if … then … else</syntaxhighlight>-branches]], and
  then ''true_statement''
+
* [[Case#case-statements|<syntaxhighlight lang="pascal" inline>case</syntaxhighlight>-statements]].
  else ''false_statement'';
 
</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.
+
== 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>]].
  
=== More statements in "if then else" statement ===
+
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.
  
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]].
+
=== 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> ===
  if boolean_condition then
+
<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.
    begin
+
<syntaxhighlight lang="pascal">if itIsMorning() then
      statement_one;
+
if itIsAHoliday() then
      statement_two;
+
begin
    end  
+
sleep;
  else
+
end
    begin
+
else
      statement_three;
+
begin
      statement_four;
+
wakeUp;
    end;
+
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>
  
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. 
+
== 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”]
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