t3x.org / t3x / t3x-manual / 273.html
 
T3X - A Minimum Procedural Language
Version 8.1.5, Online Edition
Copyright(C) 1996-2004
Nils M Holm
 
Previous:
2.7.2 Procedure Calls
TOC | Index | Back Next:
2.7.4 Loop Statements

2.7.3 Conditional Statements

There are two forms of the conditional statement. The first one is the IF statement which is avaliable in most procedural languages. Its general syntax is

IF (expression) statement

where expression may be any expression and statement may be any statement. The IF statement itself does not have to be terminated with a semicolon, since its body, which is a statement, too, already supplies the terminating semicolon. The statement which forms the body of the IF statement will be executed, if and only if expression evaluates to a `true' (non-zero) value. The following statement turns a into its absolute value:

IF (a < 0) a := -a;

If A is less than zero, then a will be replaced with -a, thereby changing its sign. Since the body a := -a is executed only, if a < 0 applies, this conditional statement always leaves a positive value in a. The semicolon in the above example belongs to the assignment.

The second form of the conditional statement is the IE statement, which implements a conditional with an alternative:

IE (expression) statement-T ELSE statement-F

Like in IF statements, any valid expression or statement may be used in the places of expression, statement-T, and statement-F.

As long as the expression yields `truth', the IE statement is equal to the IF statement, . In this case, statement-T will be executed. If the expression evaluates to `false', however, statement-F will be executed, while an IF statement would not have any effect in this case. Therefore

IE (expr) stmt ELSE ;

is equal to

IF (expr) stmt

IE is an abbreviation for If/Else. In most languages, the IF statement may or may not have an alternative. In T3X, there is a separate type of statement for each version. The reason for this choice is the `dangling else' problem which cannot arise when these statement types are separated. If no further information is supplied, the following program written in a language which allows optional alternatives would be ambiguous:

IF (condition1)
        IF (condition2) statement1
ELSE
        statement2

The problem is to decide which IF the ELSE branch belongs to: is it the alternative of IF (condition1) or IF (condition2)? In fact, most languages will bind it to the most recently opened IF - the second one in this example. In T3X, such an ambiguity does not exist:

IE (condition1)
        IF (condition2) statement1
ELSE
        statement2

Since the IF statement cannot have an alternative, the ELSE branch must belong to IE (condition1).

Previous:
2.7.2 Procedure Calls
TOC | Index | Back Next:
2.7.4 Loop Statements