If

The If statement identifies which statement(s) to run, based on the value of a Boolean expression. 

Description

In C# the If statement may 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.

> See also Else 

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# example

Below is a 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 }.

AI-Framework example

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.

 

> Read more about Else 
> Read more about ElseIf