2.04 Conditional Execution

Conditions

If ... Then ... Else construct

Description:

The ifthenelse construct is used to choose a path based on the value of one or more expressions.

The construct can be written in several different ways. The syntax description below gives a general summary.

Syntax:

if expression then [do]
:
: [commands]
:
[end]
elseif expression then [do]
:
: [commands]
:
[end]
else if expression then [do]
:
: [commands]
:
[end]
else [do]
:
: [commands]
:
[end]

where:

expression
An expression to be evaluated and tested. 0 is equivalent to false, while all other values are equivalent to true. An empty string or a null variable also returns a value of false.
commands
One or more commands. Multiple commands are enclosed in a doend construct.

Notes:

Each expression in an if or elseif statement is tested in order. As soon as an expression is found to be true, then its corresponding commands are executed. If no expressions are true, then the commands following the else keyword are executed. If else is not specified, then execution continues with the next statement.

if, else, elseif, and end must all be the first keywords on their respective lines.

if blocks may be nested.

Examples:

if x = 1 then
  put "x is 1"
else
  put "x is not 1"
...
if x = 1 then do
  put "x is 1"
  end //if x = 1
elseif x = 2 then do
  put "x is 2"
  end //if x = 2 
else if x = 3 then do
  put "x is 3"
  end //if x = 3 
else
  put "x is neither 1, 2 or 3"
...
// Single-line IF
if x = 1 then put "true" else put "false"
...

If function

Description:

If is also a function which returns a value depending on the value of an expression.

Syntax:

if(expression, true-value, false-value)

where:

expression
An expression to be evaluated and tested. 0 is equivalent to false, while all other values are equivalent to true. An empty string or a null variable also returns a value of false.
true-value
The value to be returned if the expression is true.
false-value
The value to be returned if the expression is false.

Example:

x = 0
put if(x <> 0, "true", "false") // displays false

Implementation Differences:

LynPlex Interpreter
Fully implemented.
LynPlexC Compiler
Implemented as iif.
LynPlexS Compiler
Not yet implemented.

Relational Expressions

Relational expressions are most often used in a conditional statement, but may be used anywhere that numeric expressions are allowed. A relational expression has a value of 1 if it is true and a value of 0 if it is false. Relational operations are performed from left to right, after all arithmetic operations are completed. The most usual relational operators are:

  • Equal to (=)
  • Not equal to (<>)
  • Less than (<)
  • Less than or equal to (<=)
  • Greater than (>)
  • Greater than or equal to (>=)

Boolean or Logical Expressions

Logical expressions are used for testing conditions. The logical operators AND, OR and NOT are used in combination with relational expressions to create logical expressions.

An expression is evaluated and the result is then considered true (1) or false (0). If an aritmethic expression evaluates to a zero value it is considered false. Similarly, if a string expression evaluates to a null string (or only whitespace?) it is considered false. All other values are considered true.

A logical expression using AND is true if both its left and right clauses are true.

A logical expression using OR is true if either its left or its right, or both, clauses are true.

A logical expression using NOT is true if the following clause it is not true.

See Also


lynplex/lp0204.txt · Last modified: 2012/09/08 19:12 (external edit)