RPG is both a position-oriented and free-format language. This means that certain information, such as control codes and field names, must be placed into specific positions of the RPG program statements. Failure to fulfill this obligation results in an error message. The free-format component of RPG IV is supported in the procedure area (known as the calculation specifications) and to a lesser extent in data definition.
In a free-form procedure language such as COBOL, the value of one variable (or "field") can be copied to another by specifying the following statement:
MOVE FIELDA TO FIELDB
Most high-level languages use the MOVE instruction to copy data. There is no documentation as to how this tradition started, but it's taken for granted now. The preceding COBOL statement copies each character from the memory location of the first field, FIELDA, to the memory location of the second field, FIELDB.
The same program statement written in C or C++ might look like this:
strcpy(fieldb,fielda);
In traditional RPG programs, the same program statement looks like this:
*... ... 1 ... ... 2 ... ... 3 ... ... 4 ... ... 5 ... ... 6 C MOVE FieldA FieldB
Using free-format RPG IV commands, the same program statement could look like this:
*... ... 1 ... ... 2 ... ... 3 ... ... 4 ... ... 5 ... ... 6 C Eval FieldB = FieldA
The two forms of RPG IV statements shown above are referred to as fixed format and free format. The field being copied (FIELDA) is referred to as the source field. The field that receives the data is referred to as the target or result field.
The MOVE and EVAL instructions appear in positions 26 through 35. These positions contain the program instruction or operation code. The operation code is commonly referred to as operation or opcode.
The letter "C" must appear in position 6 of RPG "Calculation" statements. Different letters are used to identify the various types of program statements (or specifications). The available specifications are:
Header specification.
File specifications.
Definition specifications.
Input specifications.
Calculation specifications.
Output specifications.
Procedure specifications.
Ordinarily, an editor is available that provides prompting for "fill-in-the-blank" coding of the different RPG specifications. Consequently, programmers need not be concerned with remembering specific positions. However, a thorough knowledge of the various specification types and their layout will greatly improve programmer efficiency.
Another example of the position-oriented structure of RPG follows. Suppose three account totals need to be accumulated. In a free-format procedure language such as PL/I, the program statements look like the source code shown in Figure 1.1.
IF ACCT = '01' THEN TOTAL1 = TOTAL1 + AMOUNT; ELSE IF ACCT = '02' THEN TOTAL2 = TOTAL2 + AMOUNT; ELSE IF ACCT = '03' THEN TOTAL3 = TOTAL3 + AMOUNT;
In traditional RPG, the equivalent program looks like the code shown in Figure 1.2.
*... ... 1 ... ... 2 ... ... 3 ... ... 4 ... ... 5 ... ... 6 C IF Acct = '01' C ADD AMOUNT TOTAL1 C ELSE C IF Acct = '02' C ADD AMOUNT TOTAL2 C ELSE C IF Acct = '03' C ADD AMOUNT TOTAL3 C ENDIF C ENDIF C ENDIF
In this example, the IF operation is used to compare the field ACCT to three numbers, and then the value of the field AMOUNT is added to one of three total fields.
RPG requires an associated ENDIF operation for each IF operation. This is because IF operations are treated as an IF THEN DO structure. This allows several statements to be conditioned and performed for each IF operation. As a by-product, the program's complexity is reduced. However, when IF statements are nested too deeply (usually more than three levels deep) the program's readability is reduced. Readability can be improved greatly, however, through the use of an in-line case structure. RPG support two forms of in-line case. The first is the SELECT-WHEN operations. The source code shown in Figure 1.2—rewritten using the SELECT-WHEN operations—is illustrated in Figure 1.3a.
*... ... 1 ... ... 2 ... ... 3 ... ... 4 ... ... 5 ... ... 6 C Select C When Acct = '01' C Add Amount Total1 C When Acct = '02' C Add Amount Total2 C When Acct = '03' C Add Amount Total3 C EndSL
The second form of in-line case is the IF statement itself. However, later versions of RPG include a new ELSEIF operation that eliminates the need for multiple ENDIF statements. The source code shown in Figure 1.2—rewritten using the SELECT-WHEN operations—is illustrated in Figure 1.3b.
*... ... 1 ... ... 2 ... ... 3 ... ... 4 ... ... 5 ... ... 6 C IF Acct = '01' C Add Amount Total1 C elseif Acct = '02' C Add Amount Total2 C elseif Acct = '03' C Add Amount Total3 C EndIf
The primary difference between these three examples is readability. The SELECT-WHEN requires only one ENDSL statement for the entire case group. The IF/ELSEIF style also requires only one ENDxx statement. The SELECT-pWHEN operations are available on all versions of RPG whereas the IF/ELSEIF option (Figure 1.3b) is only available on later releases of RPG.