Else

Else introduces the third part of the If statement, and directs to the execution of a second block of statements when the condition (the Boolean expression) is FALSE.

> See also If 

Description

In C# the If statement consists of two or three parts. The first part is the condition, expressed with a Boolean expression. This is an expression that results in a TRUE or a FALSE.

Example of a Boolean expression: a < b. This is TRUE or FALSE.

The second part is a statement or a statement block within braces { and }. This statement (or block) will be executed when the condition (the result of the Boolean expression) is TRUE.

There can also be a third part, which is a statement or a statement block within braces { and }, that will be executed when the result of the Boolean expression is FALSE. This third part is separated from the second part with the word Else. This article is about this Else statement.

Note: In C# and in the AI-Framework, the statements If, Else and ElseIf are implemented in a different way. Therefore this article shows examples of both C# and the AI-Framework.

C# examples

Below is a basic C# example of the If statement with two parts.

if (condition)
{
   then-statement;
}

The condition in line 1 is the Boolean expression, resulting in a TRUE or a FALSE. If TRUE, the then-statement(s) are executed (the first block within braces { and }). If FALSE, execution continues under the closing brace }.

Below is a C# example of the If statement with three parts, involving the Else statement.

if (condition)
{
   then-statement;
}
else
{
   else-statement;
}

The condition in line 1 is the Boolean expression, resulting in a TRUE or a FALSE. If TRUE, the then-statement(s) are executed (the first block within braces { and }). If FALSE, the else-statements are executed (the second block within braces { and }). 

AI-Framework examples

The examples above, showing the If statement with two or three parts, is coded in the AI-Framework as follows.

new If(firstName.IsEqualTo(1))
{
   ShowList("F");
},

In this example, the method ShowList() is called with the parameter "F", when the variable firstName == 1, because the Boolean expression results in TRUE.

 

new If(firstName.IsEqualTo(1))
{
   ShowList("F");
},
new Else
{
   ShowList("L");
}

In this example, the method ShowList() is called with the parameter "F", when the variable firstName == 1 (the Boolean expression). It is called with the parameter "L" when the Boolean expression is false (when the variable firstName <> 1).

 

> Read more about If 
> Read more about ElseIf