THE IF-THEN-ELSE STRUCTURE


The IF-THEN-ELSE structure is used to control the process section of a program. The Boolean comparison operator is used to control the relationship test between two operands. The available IF-THEN-ELSE Boolean operators are listed in Table 8.3.

The IF operation can test a relationship and evaluate an expression. For example, to test the relationship between two fields, the following logic applies:

     IF A = B THEN... 

To evaluate an expression within a conditional statement, the following logic applies:

     IF A = (B + C) THEN... 

To evaluate an expression, an RPG compiler generates code that calculates the result of the expression, and then uses that result to perform the comparison. For example:

     temp = B + C     IF A = temp THEN... 

The temporary result, commonly referred to as the intermediate result, is normally a floating-point numeric field. If the expression is character-string based, the intermediate result is, of course, a character field. The RPG code shown in Figure 8.8 illustrates this technique.

start figure

 .....CSRn01Factor1+++++++OpCode(ex)Factor2+++++++Result++++++++Len++DcHiLoEq       *****************************       *  If A = (B + C) then... *       ***************************** 0001 C      B            ADD       C              TEMP                7 2 0002 C      A            IFEQ      TEMP 0003 C                    . 0004 C                    . 0005 C                    . 0006 C                   ENDIF 

end figure

Figure 8.8: Coding an intermediate result for comparison.

The IFxx operation works as follows: When the relationship test between factor 1 and factor 2 is true, the statements following the IFxx operation are performed until an associated ELSE or ENDIF operation is encountered. At this point, control passes to the statement following the associated ENDIF operation. If the relationship test is false, the program branches to the associated:

  1. ELSE operation if one exists.

  2. ENDIF statement, if an ELSE operation doesn't exist.

Using the modern IF operation instead of the traditional IFxx operation, the same type of program code can be written as shown in Figure 8.9.

start figure

 .....CSRn01Factor1+++++++OpCode(ex)Factor2+++++++++++++++++++++++++++++++++++++       *****************************       *  If A = (B + C) then... *       ***************************** 0001 C                   If        A = B + C 0002 C                   . 0003 C                   . 0004 C                   . 0005 C                   ENDIF 

end figure

Figure 8.9: Using IF to eliminate the need for an intermediate result field.

The modern IF operation used in Figure 8.10 illustrates the preferred coding style. In Examples 8.1 through 8.9, you should presume fields A, B, C, and D are set as shown in Figure 8.10.

start figure

 .....DName+++++++++++EUDS.......Length+TDc.Functions++++++++++++++++++++++++++++      D A               S              5p 0 Inz(100)      D B               S              5p 0 Inz(20)      D C               S              5p 0 Inz(30)      D D               S              5p 0 Inz(40)      D Message         S             35A 

end figure

Figure 8.10: An example of preferred coding.

In Example 8.1, the relationship test between field A and the numeric constant 5 is true. Therefore, lines 2 and 3 are performed. If the relationship is false, control would pass to the associated ENDIF operation on line 4, and lines 2 and 3 would not be performed.

Example 8.1: A simple relationship test.

start example
 .....CSRn01..............OpCode(ex)Extended-factor2+++++++++++++++++++++++++++++ .....CSRn01Factor1+++++++OpCode(ex)Factor2+++++++Result++++++++Len++DcHiLoEq.... 0001 C                   If         A > 5 0002 C                   Add       20            C 0003 C                   Eval      message = 'Current' 0004 C                   endIf 
end example

In Example 8.2, the relationship test between field B and field C is false. An ELSE operation exists for this IF operation. Therefore, control passes to the ELSE operation on line 4, and lines 5 and 6 are performed. If the relationship test between fields B and C were true, however, lines 2 and 3 would be performed. Then control would pass to the ENDIF operation on line 7.

Example 8.2: A relationship test, with branching to an ELSE operation.

start example
 .....CSRn01..............OpCode(ex)Extended-factor2+++++++++++++++++++++++++++++ .....CSRn01Factor1+++++++OpCode(ex)Factor2+++++++Result++++++++Len++DcHiLoEq.... 0001 C                   If        B > C 0002 C                   ADD       10            C 0003 C                   Eval      message = 'OVERDUE' 0004 C                   ELSE 0005 C                   ADD       10            B 0006 C                   Eval      message = 'Current' 0007 C                   endIf 
end example

IFxx operations can be extended to form more complex conditioning. ANDxx and ORxx operations can be used with the IFxx operation to extend the relationship test.

In Example 8.3, the IF and AND operation on line 1 states: If A is greater than B and D and C, then perform statements 2 and 3.

Example 8.3: Testing for a list of values.

start example
 .....CSRn01..............OpCode(ex)Extended-factor2+++++++++++++++++++++++++++++ .....CSRn01Factor1+++++++OpCode(ex)Factor2+++++++Result++++++++Len++DcHiLoEq.... 0001 C                   If        A > b AND A > D AND A > C 0002 C                   Eval      message = 'A High' 0003 C                   Eval      A = 20 0004 C                   endIf 
end example

Relationship tests can be nested by using multiple IF operations. The outer-level IF-THEN-ELSE structure is tested first and, if it proves true, the next level IF-THEN-ELSE structure is tested. See Example 8.4.

Example 8.4: Compound relationship test with nesting.

start example
 .....CSRn01..............OpCode(ex)Extended-factor2+++++++++++++++++++++++++++++ .....CSRn01Factor1+++++++OpCode(ex)Factor2+++++++Result++++++++Len++DcHiLoEq.... 0001 C                   If        A > b AND A > D AND A > C 0002 C                   Eval      message = 'A High' 0003 C                   If        C < D 0004 C                   Eval      message = 'C Low' 0005 C                   Add       50            AcctBal 0006 C                   endIf 0007 C                   endIf 
end example

In Example 8.4, the same conditioning used in Example 8.3 is used to control the IF-THEN-ELSE structure. However, when line 3 is reached, if its relationship test is true, lines 4 and 5 are performed. If its relationship test is false, control passes to the ENDIF statement on line 6, ending the inner IF-THEN-ELSE structure. Additional operations can be specified between the ENDIF statements. See Example 8.5.

Example 8.5: Perform operations between ENDIF statements.

start example
 .....CSRn01..............OpCode(ex)Extended-factor2+++++++++++++++++++++++++++++ .....CSRn01Factor1+++++++OpCode(ex)Factor2+++++++Result++++++++Len++DcHiLoEq.... 0001 C                   If        A > b AND A > D AND A > C 0002 C                   Sub       10            C 0003 C                   If        D < C 0004 C                   Add       50            C 0005 C                   Eval      message = 'D Low' 0006 C                   endIf 0007 C                   ADD       20        B 0008 C                   ADD       30        C 0009 C                   ADD       40        D 0010 C                   endIf 
end example

Example 8.5 contains three ADD operations on line 7 to line 9. They are performed when the outer IF-THEN-ELSE structure is entered and after the inner IF-THEN-ELSE structure has been performed or bypassed. IF-THEN-ELSE structures can be nested to a high level (at least 100 levels with most RPG compilers). While there is no hard and fast rule, readability tends to deteriorate when more than three levels of IF-THEN-ELSE structures are nested. If more than three levels of nesting is required, the CASE structure can be used to improve readability.




The Modern RPG IV Language
The Modern RPG IV Language
ISBN: 1583470646
EAN: 2147483647
Year: 2003
Pages: 156
Authors: Robert Cozzi

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net