[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

2.3.1.6 If Control (#I)

The If command allows Cogsys to conditionally skip parts of the testlist. The first part of an If command specifies an expression to be evaluated. Constants allowed in the expression include

These may then be grouped into terms with standard relational operators. Terms may then be concatenated by logical operators into expressions. The complete grammar is enumerated below.

If statements may be nested up to IF_NESTLEVEL levels deep.

Important: To ensure fast execution, the If statement skips the true-clause by looking for a matching brace in input. It does not start running a new interpreter on the subclause. For this reason, Cogsys does not understand the context of the brace characters it encounters as it skips over a clause. Thus, any brace characters in a clause that do not delimit nested clauses will cause unexpected results, even if they are escaped or part of another command.

For example, the following code will not behave as expected:
 
#I(5>2){#G[these braces{} in true clause confuse #I]}{false clause}
To be safe, avoid braces for text display as much as possible, and never use them inside If clauses.

Name:
If
Class:
Control
Code:
#I
Syntax:
#Iexpr{true-clause}{false-clause}

White space is ignored between all tokens.

System:
The system evaluates the expression. If it is true, only the true-clause is executed; otherwise only the false-clause is executed.

History:
The If command has completely changed from the Cogsys 2.x and earlier. Previous versions had no explicit grammar and could behave very erratically depending on placement of parentheses, operators, and even whitespace. Also, nesting was not supported at all. Nevertheless, simpler #I constructs from old code will likely still work in Cogsys 3.x.

Test Files:
`IFFF_T01.IN'
tests basic operation

Examples:

input
#I(2>3 OR 4>1){This clause is true.}{This clause is false}
cogsys
Writes `This clause is true' to the output.

input

Press the letter 'A': #R
#I(K=&a O K=&A){You pressed a#I(K=&a)
{ lowercase}{n uppercase} 'A'.}{No, I said press an 'A'!}
subject
Presses A.
cogsys
Responds with `You pressed an uppercase 'A'.' .


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

This document was generated by Usman Muzaffar on June, 28 2000 using texi2html