# Conditions
Within AIML version 2.0 there exist 3 distinct types of conditions an AIMl developer can use to control/alter the flow and create loops within their grammar template processing.
* Block Conditions
* Single Predicate Condition
* Multi-predicate Condition
As is typical with most AIML tags, there are 2 variants of how the name and value pair is presented. These are either
* Attributes of the condition tag
* ``````
* Child tags of the condition tag
* ```var1val1```
We will use both options interchangeably during the tutorial pages
## Block Condition
A block condition allows you to wrap AIML template tags with a condition tag, and the children and then only evaluated if the condition is true. Each conditional value is either a global or local variable which has typically either been set previously in the processing by a set tag or has been set at startup using default values.
Each block condition is treated as a separate conditional statement, so that if a template contains multiple statements all of them are evaluated and all if true will contribute content to the template resulting evaluation.
The following examples show all the different variants representing the name/var value pair. In each example, the think tag is used to set the value of either a global ( name ) or local ( var ) variable. The value is then checked by each of the conditional statements and the appropriate text then returned as the template.
```xml
TYPE1 VARIANT1
value2
You chose
X
Y
Z
TYPE1 VARIANT2
value1
You chose
value1X
value2Y
value3Z
TYPE1 VARIANT3
value3
You chose
var1X
var1Y
var1Z
TYPE1 VARIANT4
value2
You chose
var1value1X
var1value2Y
var1value3Z
TYPE1 VARIANT1 NO MATCH
XXX
You chose
X
Y
Z
```
Running this grammar through the Bot we can ask the following questions and see the conditions in action
```bash
Loading, please wait...
No bot root argument set, defaulting to [.]
Y-Bot version 0.0.1, initiated March 14, 2017
Hi, how can I help you today?
>>> TYPE1 VARIANT1
You chose Y
>>> TYPE1 VARIANT2
You chose X
>>> TYPE1 VARIANT3
You chose Z
>>> TYPE1 VARIANT4
You chose Y
>>> TYPE1 VARIANT1 NO MATCH
You chose
```
For more information on this type of condition see [Block](./Template-Tags#type1)
## Single-predicate Condition
A single predicate condition allows you to create a construct which resembles the programming construct if then else or switch. Each li element of the condition is evaluated in turn, and processing stops as soon as the first element evaluates to true, then content of that element then used in the template. All further li elements are ignored.
If no li element evaluates to true the bot checks for a default value which is represented as an li tag without any name of value attributes/children. The content of this tag is then used.
The following examples show the various types of Single predicate condition statement.
```xml
TYPE2 VARIANT1 NO DEFAULT
value2
You chose
X
Y
Z
TYPE2 VARIANT1 WITH DEFAULT
XXX
You chose
X
Y
Z
DEF
TYPE2 VARIANT2 NO DEFAULT
value3
You chose
value1X
value2Y
value3Z
TYPE2 VARIANT2 NO MATCH
XXX
You chose
value1X
value2Y
value3Z
```
Running this grammar through the Bot we can ask the following questions and see the conditions in action
```bash
Loading, please wait...
No bot root argument set, defaulting to [.]
Y-Bot version 0.0.1, initiated March 14, 2017
Hi, how can I help you today?
>>> TYPE2 VARIANT1 NO DEFAULT
You chose Y
>>> TYPE2 VARIANT1 WITH DEFAULT
You chose DEF
>>> TYPE2 VARIANT2 NO DEFAULT
You chose Z
>>> TYPE2 VARIANT2 NO MATCH
You chose
```
For more information on this type of condition see [Single Predicate](./Template-Tags#type2)
## Multi-predicate Condition
A multi predicate condition is very much like a multi step if - elseif - elseif - else statement in most programming languages. Each condition is independent and each has their own variable and value. The bot evaluates each condition in term until one resolves to true and then use the value of the li tag as the template value.
If no condition evaluates to true then the bot looks for an li with no name and value and uses that as the default value. Otherwise if none is found no value is returned from the condition.
The following examples show the various types of Single predicate condition statement.
```xml
TYPE3 VARIANT1 NO DEFAULT
value2
You chose
A
var2B
value3C
var4value4D
TYPE3 VARIANT1 WITH DEFAULT
XXX
You chose
A
var2B
value3C
var4value4D
DEF
TYPE3 VARIANT1 NO MATCH
XXX
You chose
A
var2B
value3C
var4value4D
```
Running this grammar through the Bot we can ask the following questions and see the conditions in action
```bash
Loading, please wait...
No bot root argument set, defaulting to [.]
Y-Bot version 0.0.1, initiated March 14, 2017
Hi, how can I help you today?
>>> TYPE3 VARIANT1 NO DEFAULT
You chose A
>>> TYPE3 VARIANT1 WITH DEFAULT
You chose DEF
>>> TYPE3 VARIANT1 NO MATCH
You chose
```
For more information on this type of condition see [Multi Predicate](./Template-Tags#type3)
Default value
## Looping
Looping adds the final element to the condition structures and allows the condition to be re-evaluated repeatedly until one of the values evaluates to true.
In the first example below, the first pass through the condition, var1 = value2, this chooses the second li element, which returns 'Y' and then sets a value var1 = value3. In then also contains a loop tag, which causes the entire condition to be re-evaluated. This time as var1 = value3 the third li tag evaluates to true and the content 'Z' is returned.
In the second example, the initial value of var2 = value2. Which means the second li statement evaluates to true and B is used as the content. The li statement also contains to tags which set 2 variables, var2 = value 3 and var4 = value 4. The loop tag then forces the entire condition statement to be re-evaluated. This time the var4 = value4 forces the 4th li tag to evaluate to true this time to append D to the output.
```xml
TYPE2 LOOP
value2
You chose
X
Y value3
Z
TYPE3 LOOP
value2
You chose
A
var2
B
value3
value4
value3C
var4value4D
```
Running this grammar through the Bot we can ask the following questions and see the conditions in action
```bash
Loading, please wait...
No bot root argument set, defaulting to [.]
Y-Bot version 0.0.1, initiated March 14, 2017
Hi, how can I help you today?
>>> TYPE2 LOOP
You chose Y Z
>>> TYPE3 LOOP
You chose B D
```
By combining additional AIML elements you can implement complex conditional and looping statements such as counting elements of a list, repeat calling of an external service until it responds without error etc.
For more information on how looping works, see the documentation on [Looping](./Template-Tags#looping)
To see more information on conditional statements, see the wiki page [Conditions](./Template-Tags#condition).
***
[Back to Tutorial](./AIML-Tutorial) | [Back - Variables](./Tutorial-Variables) | [Next - Date And Time](./Tutorial-Date-And-Time)