| Prev | Next | Start of Chapter | End of Chapter | Contents | Glossary | Index | Comments | (20 out of 25)

if-then

This statement is a conditional statement that executes the then statement if G2 evaluates the truth-value-expression to true; otherwise, it executes the else statement, if one exists, or the statement following the if-then, if no else exists. The syntax is:

where:


Is any expression that evaluates to true or false. If the expression is true, G2 executes the then statement. If false, G2 executes the else expression if one exists, or transfers control to the statement following the if statement.

Is any procedure or action statement.

For example:

In this example, the program returns 10 if x equals y; however, if x does not equal y, the program returns 12.

You can nest if-then statements by enclosing the nested if-then statement in a begin-end block. For example:

If x is not equal to y, G2 ignores the nested if-then statement and passes control to the else portion of the outer if statement. If x does equal y, G2 executes the nested if-then statement.


Note: When a then clause contains a begin-end block immediately followed by an else clause, do not put a semicolon after the keyword end. Such a semicolon would terminate the scope of the if statement, causing the else clause to appear as a syntax error.

| Prev | Next | Start of Chapter | End of Chapter | Contents | Glossary | Index | Comments | (20 out of 25)

Copyright © 1997 Gensym Corporation, Inc.