Presentation © Copyright 2002, Bryan Meyers Arithmetic and Assignment Operations Chapter 4.

Slides:



Advertisements
Similar presentations
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Advertisements

Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
Introduction to C Programming
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
7-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Expressions and Operators Program Style.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
Chapter 3 Numerical Data. Topics Variables Numeric data types Assignment Expressions.
Introduction to C Programming
1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral.
Expressions, Data Conversion, and Input
Objectives You should be able to describe: Data Types
Presentation © Copyright 2002, Bryan Meyers Defining Data with Definition Specifications Chapter 3.
Getting Started Chapter 2 Presentation © Copyright 2002, Bryan Meyers
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Presentation © Copyright 2002, Bryan Meyers Externally Described Files Chapter 6.
Arithmetic operations and operators, converting data types and formatting programs for output. Year 11 Information Technology.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
A First Book of ANSI C Fourth Edition Chapter 3 Processing and Interactive Input.
D-1 University of Washington Computer Programming I Lecture 4: Arithmetic Expressions © 2000 UW CSE.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 2 Input,
Input, Output, and Processing
7-1 Structured COBOL Programming Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout John.
Presentation © Copyright 2002, Bryan Meyers Top-Down, Structured Program Design Chapter 5.
7-1 Chapter 7.  Basic Arithmetic Verbs  Options Available with Arithmetic Verbs  COMPUTE Statement  Signed Numbers in Arithmetic Operations  Intrinsic.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Chapter 3 Assignment, Formatting, and Interactive Input C++ for Engineers and Scientists Third Edition.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
Lecture 41 Arithmetic Clauses The ROUNDED clause –Place ROUNDED after the variable that holds the result ADD A TO B ROUNDED. ADD A TO B GIVING C ROUNDED.
Welcome to RPG544. Bit about Cindy Administrative Stuff Standards Due Dates Web Page.
Chapter 3: Assignment, Formatting, and Interactive Input.
C++ for Engineers and Scientists Second Edition Chapter 3 Assignment, Formatting, and Interactive Input.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
IFS Intro to Data Management Chapter 5 Getting More Than Simple Columns.
7-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)
Significant Figure Rules RulesExamples The following are always significant Non zero digits Zeros between non zero digits Zero to the right of a non zero.
1 Chapter 5 – The Procedure Division File handling statements –OPEN statement Initiates processing for a file Input Output Each file opened must have been.
Chapter 2 Variables.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
Programming with Microsoft Visual Basic th Edition
Doing math In java.
Chapter 3 Assignment, Formatting, and Interactive Input C++ for Engineers and Scientists Third Edition.
Introduction ABAP Fields and Variables. Slide 2 Fields (Introduction) In ABAP, fields (or data objects) are named locations in memory Variables store.
D-1 University of Washington Computer Programming I Lecture 4: Arithmetic Expressions © 2000 UW CSE.
CSCI 1100/1202 January 18, Arithmetic Expressions An expression is a combination of operators and operands Arithmetic expressions compute numeric.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
1 Lecture Three I/O Formatting and Arithmetic Dr. Sherif Mohamed Tawfik.
Making Interactive Programs with Visual Basic .NET
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Arithmetic operations and operators, converting data types and formatting programs for output. Year 11 Information Technology.
© 2016, Mike Murach & Associates, Inc.
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Multiple variables can be created in one declaration
The Selection Structure
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Lecture 3 Expressions Richard Gesick.
A First Book of ANSI C Fourth Edition
CIS16 Application Development Programming with Visual Basic
Computing in COBOL: The Arithmetic Verbs and Intrinsic Functions
THE COMPUTE STATEMENT Purpose: performs mathematical calculations
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Data Types and Expressions
Engineering Problem Solving with C++ An Object Based Approach
Data Types and Expressions
Data Types and Expressions
Presentation transcript:

Presentation © Copyright 2002, Bryan Meyers Arithmetic and Assignment Operations Chapter 4

Programming in RPG IV Third Edition 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

Programming in RPG IV Third Edition 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 –Evaluates the expression to the right of the equal sign –Stores value in the field to the left of the equal sign Coding EVAL is optional with Version 5 free-format * CL0N01Factor Opcode(E)+Extended-factor // Free-format (Version 5 or later) /FREE EVAL Result_field = expression; /END-FREE // Fixed-format (pre-Version 5) C EVAL Result_field = expression

Programming in RPG IV Third Edition 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

Programming in RPG IV Third Edition 5 Determining Field Sizes Arithmetic Operation Rule of ThumbExample AdditionResult should be one digit longer than longest factor SubtractionResult should be as long as longest factor MultiplicationResult field length should be the sum of the digit lengths of the factors 999 x DivisionResult field should be the sum of the digit length of the factors 999 /.1 = 9990

Programming in RPG IV Third Edition 6 Rounding (Half-Adjusting) To round, place an ‘H’ in the operation extender on the EVAL –Free-format specification requires explicit EVAL when using an extender (Version 5) Round only when necessary –The RPG compiler will issue a warning message about unnecessary half-adjusting –Unnecessary rounding reflects poorly on your programming skills and/or style * /FREE EVAL(H) Interest = LoanAmt * StdRate; EVAL(H) Interest = AltAmt * PrimeRate; /END-FREE

Programming in RPG IV Third Edition 7 Improving EVAL Precision To ensure that EVAL doesn’t truncate significant decimal places, use “Result Decimal Positions” option for expressions –Specify EXPROPT(*RESDECPOS) in H spec, to set default option for program –Or, specify (R) extender for a single EVAL statement Otherwise system defaults to EXPROPT(*MAXDIGITS) option * H EXPROPT(*RESDECPOS) /FREE EVAL(HR) Pay = HourlyRate * * HourlyRate *(HoursWorked -40); /END-FREE

Programming in RPG IV Third Edition 8 Sample Program - F Specs * // ********************************************************************** // This program calculates selling prices for received items based on a // 60% markup over item cost. It also determines projected gross profit // for each item in the shipment and total gross profit for all the items // ********************************************************************** FNewItems IF F 40 DISK FQPRINT O F 132 PRINTER

Programming in RPG IV Third Edition 9 Sample Program - D & I * // Definition of all work fields used in program D UnitProfit S 6 2 D SellPrice S 7 2 D GrssProfit S 10 2 D TotProfit S 14 2 // Input file of received items defined within the program INewItems NS I 1 30 Descript I ItemCost I QtyRcvd

Programming in RPG IV Third Edition 10 Sample Program - C Specs * /FREE EXCEPT Headings; READ NewItems; DOW NOT %EOF; EVAL(H) UnitProfit = ItemCost *.6; SellPrice = ItemCost + UnitProfit; GrssProfit = UnitProfit * QtyRcvd; TotProfit = GrssProfit + TotProfit; EXCEPT Detail; READ NewItems; ENDDO; EXCEPT TotalLine; *INLR = *ON; RETURN; /END-FREE

Programming in RPG IV Third Edition 11 Sample Program - O Specs * OQPRINT E Headings 1 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’

Programming in RPG IV Third Edition 12 Sample Program - O Specs * O E Detail 1 O Descript 30 O ItemCost 1 41 O SellPrice 1 53 O QtyPcvd 1 62 O UnitProfit 1 74 O GrssProfit 1 92 O E TotalLine 1 O 70 'GRAND TOTAL GROSS PROFIT - O PROJECTED:' O TotProfit 1 92 '$'

Programming in RPG IV Third Edition 13 Character Assignment EVAL is used in character assignment –Both sides of assignment expression must be character fields or expressions Use a continuation character (+ or -) to continue a literal 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 May also use figurative constants –*BLANKS, *HIVAL, *LOVAL, etc.

Programming in RPG IV Third Edition 14 Data Type Conversion: MOVE MOVE operation transfers characters from Factor 2 field to the Result, character by character, moving through the fields from right to left –MOVEL moves from left to right Disregards decimal position in numeric fields To pad extra result positions with blanks, use (P) extender –Good practice: match the size of the Result and Factor 2 fields MOVE not supported by free-format * CL0N01Factor Opcode(E)+Factor Result Len++D+HiLoEq.... * Store a numeric value in character field Alpha (length 4) C MOVE 1234 Alpha * Store character field Alpha in a numeric field (length 4, 0 decimals) C MOVE Alpha Numeric

Programming in RPG IV Third Edition 15 Built in Functions (BIF) %ABS (Absolute Value) –Returns absolute value of expression, discarding sign %DIV (Divide) –Integer division %REM (Remainder) –Determines remainder from integer division (modulus) * /FREE TransTotal = %ABS(Debits - Credits); Hours = %DIV(Minutes:60); Minutes =%REM(Minutes:60); /END-FREE

Programming in RPG IV Third Edition 16 Points to Remember EVAL operation can express complex arithmetic calculations in a single free-form 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 –Not support by free-format Built-in functions perform complex calculations, string operations, and data-type conversion in free-format expressions