Appendix: Compound Statements
Expressions created in KnowledgeKube tend to consist of one or more single-line statements separated by semicolons. These statements are activated consecutively when the expression is called, and typically perform very specific, self-contained tasks.
A Compound Statement is different, in that it features a clearly-defined 'block' of code that is executed in sequence whenever a headline condition is satisfied. This type of statement lets you group several expressions together and execute them together under controlled circumstances.
This is similar to a standard If statement, which uses the following syntax:
If( SomethingIsTrue, DoThis, OtherwiseDoThis );
While this has many uses, creating complex If statements can result in expressions that span multiple lines and are difficult to both read and maintain.
Using a compound statement, you can make complex If statements significantly easier to manage. Because the statement is executed as a block, it also lets you perform far more advanced actions when the condition is satisfied, with significantly less effort.
An example of a compound If statement is as follows:
If:(SomethingIsTrue)
{
DoThis;
ThenDoThis;
AlsoDoThis;
}
The first line of this example constitutes the headline condition, while the subsequent text inside the curly brackets (braces) represents the code that will be executed if the condition is satisfied. If the condition is not satisfied, the expression engine will ignore the code inside the braces entirely.
As with other expressions, compound statements can be nested inside one another. The order in which brackets are opened and closed is just as important here, but this type of nesting also requires that you correctly close each set of braces.
If care is not taken when closing braces, the compound statement will either run incorrectly, or not run at all. The following is an example of a properly-nested pair of compound statements:
If:(ActionType="Purchase")
{
ValidateCardDetails();
If:(ProductStock >= PurchaseQuantity)
{
ProcessPurchase();
}
}
Notice that since the second If statement is written before the closing braces of the first one, it will only be evaluated if the first condition is True. In this example, the ProcessPurchase function will not trigger unless both conditions are True, whereas the ValidateCardDetails function will always trigger as long as the first condition is True.
Adding an Else clause expands the statement to include an alternative result, which will be executed if the expression equates to False:
If:(SomethingIsTrue)
{
DoThis;
}
else
{
DoThisInstead;
}
You can also cause a compound statement to finish execution early by using a Stop or Exit statement. Doing this is called Terminating a statement.
In addition to compound If statements, KnowledgeKube contains the following types of compound statement:
Statement Type | Description | Further Reading |
---|---|---|
While Loop | Repeatedly executes a block of code for as long as a specific condition is met. | Appendix: While Loops |
Labelled | Gives a unique reference to a block of code. This code can then be jumped directly to and executed by calling the unique reference. | Appendix: Labelled Statements. |