Rice Lake iRite IDE User Manual
Page 28
24
920i
Programming Reference
The if-else is used when the program must decide which of exactly two different paths of execution must be
executed. The path that will execute the statement or statements in <statement list 1> will be chosen if
<expression> evaluates to true. Here is an example:
if iAge => 18 then
sStatus := "Adult";
else
sStatus := "Minor";
end if;
If the statement is false, then the statement or statements in <statement list 2> will be executed. Once the
expression is evaluated and one of the paths is chosen, the expression is not evaluated again. This means the
statement will terminate after one of the paths has been executed.
For example, if the expression was true and we were executing <statement list 1>, and within the code in
<statement list 1> we change some part of <expression> so it would at that moment evaluate to false,
<statement list 2> would still not be executed. This point is more relevant in the next form called the if-elsif.
ELSIF
expr
stmt-list
THEN
Figure 3-14. Optional Else-If Statement Syntax
The if-elsif version is used when a multi-way decision is necessary and has this general form:
if <expression> then
<statement list 1>
elsif <expression> then
<statement list 2>
elsif <expression> then
<statement list 3>
elsif <expression> then
<statement list 4>
else
<statement list 5>
end if;
Here is an example of the if-elsif form:
if rWeight <= 2.0 then
iGrade := 1;
elsif (rWeight > 2.0) and (rWeight < 4.5) then
iGrade := 2;
elsif (rWeight > 4.5) and (rWeight < 9.25) then
iGrade := 3;
elsif (rWeight > 9.25) and (rWeight < 11.875) then
iGrade := 4;
else
iGrade := 0;
sErrorString := "Invalid Weight!";
end if;