Difference between revisions of "Else"

From Lazarus wiki
Jump to navigationJump to search
m
m
Line 1: Line 1:
 +
{{else}}
 +
<br>
 
Else is [[Keyword|keyword]] which introduces the action to do if the condition is [[False|false]].
 
Else is [[Keyword|keyword]] which introduces the action to do if the condition is [[False|false]].
  

Revision as of 17:40, 1 March 2013

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

Else is keyword which introduces the action to do if the condition is false.

If then else

  if (condition)
  then ''true_statement''
  else ''false_statement'';

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.

More statements in "if then else" statement

If you need two or more statements for true_statement or false_statement, then the group of statements must be placed within a begin ... end Block.

  if boolean_condition then
    begin
      statement_one;
      statement_two;
    end 
  else
    begin
      statement_three;
      statement_four;
    end;

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:

  if a then
      if b then 
        begin
           (..)
        end;
      else
        begin
           (..)
        end;

In this case, the "else" applies to "if a"

  if a then
      if b then 
        begin
           (..)
        end
      else
        begin
           (..)
        end;

In this case, the "else" applies to "if b". If this causes ambiguity, it can be resolved by coding an "empty" else statement:

  if a then
      if b then 
        begin
           (..)
        end
      else
  else
      begin
           (..)
      end


Keywords: begindoelseendforifrepeatthenuntilwhile