1 / 18

RPG IV

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

zitomira
Download Presentation

RPG IV

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. RPG IV Arithmetic and Assignment Operations - Chapter 4

  2. 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

  3. 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.”

  4. 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

  5. Determine Field Sizes

  6. 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.

  7. 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

  8. 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

  9. Sample Program - C Specs C************************************************************************** C* Initialization * C* Print headings and priming read. * C************************************************************************** C EXCEPT Headings C READ NewItems 90

  10. 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

  11. Sample Program - C Specs C************************************************************************** C* Termination * C* Print grand total and end program * C************************************************************************** C EXCEPT TotalLine C EVAL *INLR = *ON C RETURN

  12. 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’

  13. 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 '$'

  14. 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

  15. 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.

  16. 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

  17. Built in Functions(BIF) • %ABS(Absolute Value) • %DIV(Divide) • %REM(Remainder)

  18. 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

More Related