[ < ] [ > ]   [ << ] [ 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 can be nested up to 16 levels deep.

Note: The brace closing the "if true" clause does not need be on the same line as its corresponding opening brace. However, the closing brace of the "if true" clause MUST be on the same line as the opening brace of the "else" clause. Also, you cannot have an "if true" clause without an "else" clause, but either clause may be empty.

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

 
@CPress any key to enter two integer values to
determine if the first is greater than the second
(\#I(V10>V11) ...)#R

Enter first integer number  (V10): $GV10
Enter second integer number (V11): $GV11

#I(V10>V11) {$SV10 > $SV11} {$SV10 <= $SV11}

Press 'a'#R

#I(K=&a O K=&A) {You pressed 'a'.} {You did not press 'a'.}

Press 'b'#R

#I(K=&a O K=&A) {You pressed 'a'.} {You did not press 'a'.}

Done.  Press any key to exit.#R

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 on November, 24 2008 using texi2html