Branching Constructs
[ The if statement ] [ The if-then-else statement ]
The if and if-then-else statements provide branching control in ALADDIN's problem solving procedures.
Syntax : The if statement syntax is:
if ( logical expression ) {
statement 1 ;
statement 2 ;
......
statement N ;
}
Statements 1 through N will be executed only if the logical expression evaluates to "true." Readers should also note that unlike the C programming language, the braces are required even if N equals 1.
Example 1 : The script
x = 1 ksi;
if ( x < 10 ksi ) {
print " x = ", x ,"\n";
}
generates the output
x = 1 ksi
Example 2 : Logical expressions can be a composition of logical and relational operands. For example, the script:
time = 20 sec;
if( time > 0 sec && time <= 100 sec ) {
print " time = ", time, "\n";
}
generates the output
time = 20 sec
Example 3 : ALADDIN checks compatibility of units before attempting to evaluate the logical expression. The run-time execution of the script
x = 1 ksi;
if ( x < 10 cm ) {
print " x = ", x ,"\n";
}
fails because the units of x
and 10 cm are incompatible.
Syntax : The if-then-else statement syntax is:
if ( logical expression ) then {
statement 1 ;
......
statement K ;
} else {
statement K+1 ;
......
statement N ;
}
Statements 1 through K will be executed if the logical expression evaluates to "true." Otherwise, statements K+1 through N will be executed.
Example 4 : The script
x = 10 ksi; y = 1 MPa;
if ( x < 10 ksi ) then {
print " x = ", x ,"\n";
} else {
print " y = ", y ,"\n";
}
generates the output
y = 1 MPa