Difference between revisions of "Else"
m (Text replace - "delphi>" to "syntaxhighlight>") |
|||
Line 2: | Line 2: | ||
= [[If]] [[Then|then]] else = | = [[If]] [[Then|then]] else = | ||
− | < | + | <syntaxhighlight> |
if (condition) | if (condition) | ||
then ''true_statement'' | then ''true_statement'' | ||
else ''false_statement''; | 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. | 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. | ||
Line 14: | Line 14: | ||
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]]. | 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]]. | ||
− | < | + | <syntaxhighlight> |
if boolean_condition then | if boolean_condition then | ||
begin | begin | ||
Line 25: | Line 25: | ||
statement_four; | statement_four; | ||
end; | 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. | 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. | ||
Line 31: | Line 31: | ||
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: | 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 a then | ||
if b then | if b then | ||
Line 41: | Line 41: | ||
(..) | (..) | ||
end; | end; | ||
− | </ | + | </syntaxhighlight> |
In this case, the "else" applies to "if a" | In this case, the "else" applies to "if a" | ||
− | < | + | <syntaxhighlight> |
if a then | if a then | ||
if b then | if b then | ||
Line 53: | Line 53: | ||
(..) | (..) | ||
end; | 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: | 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 a then | ||
if b then | if b then | ||
Line 66: | Line 66: | ||
(..) | (..) | ||
end | end | ||
− | </ | + | </syntaxhighlight> |
{{Keywords}} | {{Keywords}} |
Revision as of 15:08, 24 March 2012
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: begin — do — else — end — for — if — repeat — then — until — while