Branching Statements

Branching Statements

For our first complicated PSM statement type, let us examine the if-statement. The form is only a little strange; it is different from C or similar languages in that:

1. The statement ends with keywords END IF.

2. If-statements nested within the else-clause are introduced with the single word ELSEIF.

Therefore, the general form of an if-statement is as suggested by Figure 1. The condition is any boolean-valued expression, as can appear in the WHERE clause of SQL statements. Each statement list consists of statements ended by semicolons, but does not need a surrounding BEGIN...END. The final ELSE and its statement(s) are optional; i.e., IF...THEN...END IF alone or with ELSEIF's is acceptable.

The form of an if statement

Example 1. Let us write a function to take a year y and a studio s, and return a boolean that is TRUE if and only if studio s produced at least one black-and-white movie in year y or did not produce any movies at all in that year. The code appears in Figure 2.

If there are any movies at all then at least one has to be in black and white
Line(1) introduces the function and includes its arguments. We do not need to specify a mode for the arguments, since that can only be IN for a function. Lines (2) and (3) test for the case where there are no movies at all by studio s in year y, in which case we set the return-value to TRUE at line (4). Note that line (4) does not cause the function to return. Technically, it is the flow of control dictated by the if-statements that causes control to jump from line (4) to line (9), where the function completes and returns.

If studio s made movies in year y, then lines (5) and (6) test if at least one of them was not in color. If so, the return-value is again set to true, this time at line (7). In the remaining case, studio s made movies but only in color, so we set the return-value to FALSE at line (8).