180 likes | 214 Views
RPG IV. Arithmetic and Assignment Operations - Chapter 4. Objectives :. Perform arithmetic calculations in your programs Determine the appropriate size for numeric fields Determine how to round calculations to avoid truncation Convert data types
E N D
RPG IV Arithmetic and Assignment Operations - Chapter 4
Objectives: • Perform arithmetic calculations in your programs • Determine the appropriate size for numeric fields • Determine how to round calculations to avoid truncation • Convert data types • Use numeric and character literals; and figurative constants
Simple Numeric Assignment • Assigning a value to a field simple means giving a field a value. • EVAL is used to assign values to fields • An EVAL statement says, in effect, “Evaluate the expression to the right of the equal sign and store its value in the result field to the left of the equal sign.”
Using EVAL for Arithmetic • Single expression can contain as many arithmetic operators, numeric literals, and numeric fields to accomplish a desired calculation • Left of ‘=‘ is a numeric field, to the right can be numeric fields or numeric literals • Rules of precedence are used to determine order of math operations
Rounding or Half-Adjust • To round place an ‘H’ in the operation extender on the EVAL • Round only when necessary. • The RPG compiler will issue a warning message about unnecessary half-adjusting. • Rounding when uncalled for reflects poorly on your programming skills and/or style.
Sample Program - F Specs *..1....+....2....+....3....+....4....+....5....+....6....+....7....+....8 F************************************************************************** F* This program calcualtes selling prices for received items based on a * F* 60% markup over item cost. It also determins projected gross profit * F* for each item in the shipment and total gross profit for all the items* F* Author: J. Yaeger * F* Date Writtem: Jan. 1995 * F************************************************************************** *..1....+....2....+....3....+....4....+....5....+....6....+....7....+....8 F*ilename++IP FRlen+ Device * FNewItems IF F 40 DISK FQPRINT O F 132 PRINTER
Sample Program - D & I Specs D************************************************************************** D* Definition of all work fields used in program. * D************************************************************************** *..1....+....2....+....3....+....4....+....5....+....6....+....7....+....8 D*Name++++++++++ Ds Len+ Dc D UnitProfit S 6 2 D SellPrice S 7 2 D GrssProfit S 10 2 D TotProfit S 14 2 I************************************************************************** I* Input file of received items defined within the program. * I************************************************************************** *..1....+....2....+....3....+....4....+....5....+....6....+....7....+....8 I*ilename++Sq INewItems NS *..1....+....2....+....3....+....4....+....5....+....6....+....7....+....8 I* From+To+++DcField+++++++++ I 1 30 Descript I 31 36 2ItemCost I 37 40 0QtyRcvd
Sample Program - C Specs C************************************************************************** C* Initialization * C* Print headings and priming read. * C************************************************************************** C EXCEPT Headings C READ NewItems 90
Sample Program - C Specs C************************************************************************** C* Mainline * C* Perform loop until endof file. * C* Calculate per unit profit, selling price, total gross profit * C* Accumulate grand total, Print detail line, Read next record * C************************************************************************** C DOW %NOT EOF C EVAL (H) UnitProfit = ItemCost * .6 C EVAL SellPrice = ItemCost + UnitProfit C EVAL GrssProfit = UnitProfit * QtyRcvd C EVAL TotProfit = GrssProfit + TotProfit C EXCEPT Detail C READ NewItems 90 C ENDDO
Sample Program - C Specs C************************************************************************** C* Termination * C* Print grand total and end program * C************************************************************************** C EXCEPT TotalLine C EVAL *INLR = *ON C RETURN
Sample Program - O Specs *..1....+....2....+....3....+....4....+....5....+....6....+....7....+....8 O*ilename++D ExceptnameB++A++Sb+Sa+ OQPRINT E Headings 1 *..1....+....2....+....3....+....4....+....5....+....6....+....7....+....8 O* Field+++++++++Y End++ Constant/Editword+++++++++++ O UDATE Y 11 O 55 'GROSS PROFIT PROJECTION' O 82 'PAGE' O PAGE 87 O E Headings 2 1 O 52 'SELLING' O 74 'PER UNIT' O 88 'TOTAL' O E Headings 2 O 10 'ITEM' O 39 'COST' O 51 'PRICE' O 61 'QTY' O 73 'PROFIT' O 92 'GROSS PROFIT’
Sample Program - O Specs *..1....+....2....+....3....+....4....+....5....+....6....+....7....+....8 O*ilename++D ExceptnameB++A++Sb+Sa+ O E Detail 1 *..1....+....2....+....3....+....4....+....5....+....6....+....7....+....8 O* Field+++++++++Y End++ Constant/Editword+++++++++++ O Descript 30 O ItemCost 1 41 O SellPrice 1 53 O QtyPcvd 1 62 O UnitProfit 1 74 O GrssProfit 1 92 *..1....+....2....+....3....+....4....+....5....+....6....+....7....+....8 O*ilename++D ExceptnameB++A++Sb+Sa+ O E TotalLine 1 *..1....+....2....+....3....+....4....+....5....+....6....+....7....+....8 O* Field+++++++++Y End++ Constant/Editword+++++++++++ O 70 'GRAND TOTAL GROSS PROFIT - O PROJECTED:' O TotProfit 1 92 '$'
Character Assignment • EVAL is used in character assignment • Use a continuation character (+ or -) to continue on the next line • EVAL operation performs the assignment by transferring character by character, starting with the left-most character of the literal. • The result field is either padded with blanks or truncated
Data Type Conversion: MOVE • Move operation transfers characters from the sending field in Factor 2 to the receiving field in the Result, character by character, moving thru the fields from right to left. • When changing data types with a MOVE operation it is good programming practice to match the size of the result field with the size of the factor 2 value.
Data Type Conversion: MOVEL • The MOVEL operation requires a Factor 2 and a Result entry • Works like a MOVE operation • Moves data, character by character, from left to right • The data transfer starts with the left-most characters of the sending field and receiving field
Built in Functions(BIF) • %ABS(Absolute Value) • %DIV(Divide) • %REM(Remainder)
Points to Remember • EVAL operation can express complex arithmetic calculations in a single free-from expression • Truncation is based on the location of the decimal positions • EVAL is used to assign values to character fields • Operations MOVE and MOVEL are used to change data type