THE COMPUTE STATEMENT Purpose: performs mathematical calculations

Slides:



Advertisements
Similar presentations
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)
Advertisements

Computer Science 1620 Arithmetic. C++ Math we have seen how to use numbers in our program to represent data however, we can also manipulate this data.
1 Expressions, Operators Expressions Operators and Precedence Reading for this class: L&L, 2.4.
Exponent Rules – Day 1 Zero and Negative Exponents.
Copyright © 2003 Pearson Education, Inc. Slide 2-1 Problem Solving with Java™ Second Edition Elliot Koffman and Ursula Wolz Copyright © 2003 Pearson Education,
Chapter 5 Using Data and COBOL Operators. Initializing Variables When you define a variable in WORKING- STORAGE, you also can assign it an initial value.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
A First Book of ANSI C Fourth Edition Chapter 3 Processing and Interactive Input.
7-1 Structured COBOL Programming Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout John.
Review of Using Exponents
7-1 Chapter 7.  Basic Arithmetic Verbs  Options Available with Arithmetic Verbs  COMPUTE Statement  Signed Numbers in Arithmetic Operations  Intrinsic.
Assignment Statements Operator Precedence. ICS111-Java Programming Blanca Polo 2 Assignment, not Equals  An assignment statement changes the value of.
Definitions Add & Subtract Multiply & Divide ExponentsMixed.
Arithmetic Operations. Review function statement input/output comment #include data type variable identifier constant declaration.
Order of Operations.
Mathematical Calculations in Java Mrs. G. Chapman.
Write the following statement as a mathematical expression using parenthesis and brackets and then evaluate. 51. Multiply 5 by 3.From this product.
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.
Divide. Evaluate power – 3 = – 3 EXAMPLE – 3 = 3 2 – – 3 = 6 – 3 Multiply. Evaluate expressions Multiply and divide from.
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)
Any Questions? Agenda Level 77 Initialize Display & Accept Arithmetic Verbs Compute statement String/Unstring Inspect.
Mathematical Calculations in Java Mrs. C. Furman.
Unit 2: Integers Unit Review. Multiplying Integers The product of two integers with the same sign is a positive. Eg: (+6) x (+4) = +24; (-18) x (-3) =
Arithmetic OperatorOperationExample +additionx + y -subtractionx - y *multiplicationx * y /divisionx / y Mathematical FormulaC Expressions b 2 – 4acb *
This will all add up in the end. Assignment operator =Simple Assignment operator Arithmetic Operators +Additive operator – Subtraction operator * Multiplication.
1.2 Words and Expression.
CMSC 104, Version 8/061L10ArithmeticOps.ppt Arithmetic Operators Topics Arithmetic Operators Operator Precedence Evaluating Arithmetic Expressions In-class.
Dividing Polynomials. First divide 3 into 6 or x into x 2 Now divide 3 into 5 or x into 11x Long Division If the divisor has more than one term, perform.
Expressions and Order of Operations Operators – There are the standard operators: add, subtract, divide, multiply – Note that * means multiply? (No times.
Any Questions! 2 Agenda Moving data Arithmetic Verbs Compute statement.
Equations Inequalities = > 3 5(8) - 4 Numerical
Mathematics Section Numbers and Operations Measurement Data Interpretation Algebra Calculators are not allowed on the test!
7.13 – Algebraic Expressions & Equations How can algebraic expressions and equations be written? Word phrases and sentences can be used to represent algebraic.
Slide Copyright © 2009 Pearson Education, Inc. Welcome to MM 150 Survey of Mathematics.
Positive and Negative Numbers
Thinking Mathematically
Operations with Integers
Assignment statement:
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
Assignment and Arithmetic expressions
Variables, Expressions, and IO
Assignment statement and Arithmetic operation 2
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
1-1: Writing Expressions and Equations
Arithmetic Operator Operation Example + addition x + y
Lecture 3 Expressions Richard Gesick.
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
Number and String Operations
A First Book of ANSI C Fourth Edition
Review of Using Exponents
Arithmetic Expressions & Data Conversions
Section 5.3 The Rational Numbers
Two Step Equation.
Equation with variables on both sides
Computing in COBOL: The Arithmetic Verbs and Intrinsic Functions
One step equation with Addition and Subtraction
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
Two step equation Operations
Dividing Polynomials.
Two step equation Brackets
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
15.
is multiplied by a variable of type to yield a result of type
Data Types and Expressions
Review Simplify 8 – 120 ÷ 5 • ÷ – 2 | 5 – 7 |
Arithmetic Expressions & Data Conversions
Subtract unlike rational numbers
Data Types and Expressions
Presentation transcript:

THE COMPUTE STATEMENT Purpose: performs mathematical calculations Syntax Definition: COMPUTE {data-name [ROUNDED]}… = arithmetic-expression [ON SIZE ERROR stmt-group1] [NOT ON SIZE ERROR stmt-group2] [END-COMPUTE] Symbol Reminder: + - * / ** also ( ) Examples: COMPUTE total-cost = total-cost + meal-cost compute num-meals = num-meals + 1 compute tot-meal-cost = meal-cost * num-cust compute a = b + c / d For Your Information This statement calculates the arithmetic expression to the right of the = sign and stores the result in the variable(s) to the left of the = sign ALL variables in the arithmetic expression must be numeric items (i.e. not edited) The result variable (i.e. data-name) can be numeric or numeric-edited The ON SIZE ERROR is executed if the result is larger than the receiving field *** SUBTRACTION ISSUE! Arithmetic Syntax

THE ARITHMETIC EXPRESSION Symbol Reminder: + - * / ** also ( ) Precedence: just like evaluating math problems () inside/out ** from right to left * / from left to right + - from left to right Examples: a=10, b=5, c=3, d=2, e = 4 Compute x = a / b * c Compute y = b + a – d / d ** c Compute z = a / d – e / d * c For Your Information MOVE rules apply here when storing the arithmetic expression into the result memory location. Put in parenthesis and/or take out parenthesis to determine if the equations is still evaluated the same way. Don’t forget that each variable to the right of the = sign must have a value prior to the execution of the statement Watch out for divide by 0 error… is a run error! Arithmetic Syntax

THE ADD STATEMENT Syntax Definition: FORMAT 1: ADD {identifier-1 | literal-1} ... TO {identifier-2 [ROUNDED]}... FORMAT 2: TO {identifier-2 | literal-2} GIVING {identifier-3 [ROUNDED]} ... [ON SIZE ERROR stmt-group1] [NOT ON SIZE ERROR stmt-group2] [END-ADD] Examples: a=1, b=2, c=3, d=4 Add a b 3 TO c d Add a b 3 to d giving e Add a d giving c For Your Information All literals and fields that are part of the addition must be numeric. The field after the word GIVING may be edited. The result field, following the word TO or the word GIVING, must be a data-name, not a literal. The result field is the only field changed as a result of the ADD operation. When using the TO format, the contents of the receiving field are part of the ADD operation and are added along with the other fields. When using the GIVING format, the result field is NOT part of the ADD operation Notice in Format 2 that the TO is not required. In Format 2, notice that the value after TO is singular FYI: Example 2 is only allowed by the COBOL 85 compiler. FYI: Convert to equivalent COMPUTE statements Arithmetic Syntax

THE SUBTRACT STATEMENT Syntax Definition: FORMAT 1: SUBTRACT {identifier-1 | literal-1} ... FROM {identifier-2 [ROUNDED]}... FORMAT 2: FROM {identifier-2 | literal-2} GIVING {identifier-3 [ROUNDED]} ... [ON SIZE ERROR stmt-group1] [NOT ON SIZE ERROR stmt-group2] [END-SUBTRACT] Examples: a=1, b=2, c=4, d=8 Subtract a b from d. Subtract a b from c giving d. Subtract a b from c d For Your Information The receiving field after the word GIVING may be edited. The receiving field must be a data-name, not a literal. All the fields and literals preceding the word FROM are ADDED together and the sum is subtracted from the field following the word FROM. When using the GIVING format, the result of the subtraction is placed in the field following the word GIVING. The initial contents of the receiving field do not take part in the arithmetic operation. Arithmetic Syntax

THE MULTIPLY STATEMENT Syntax Definitions: FORMAT 1: MULTIPLY {identifier-1 | literal-1} BY {identifier-2 [ROUNDED]}… FORMAT 2: MULTIPLY {identifier-l | literal-l} BY {identifier-2 | literal-2} GIVING {identifier-3 [ROUNDED]} ... [ON SIZE ERROR stmt-group1] [NOT ON SIZE ERROR stmt-group2] [END-MULTIPLY] Examples: a=1, b=2, c=4, d=8 Multiply b by c d. Multiply b by c giving d For Your Information The receiving field after the word GIVING may be edited. The receiving field must be a data-name, not a literal. Make sure the receiving field is large enough to hold the result. Arithmetic Syntax

THE DIVIDE STATEMENT Syntax Definitions: FORMAT 1: For Your Information The receiving field after the word GIVING may be edited. The receiving field must be a data-name, not a literal. The BY option must have a GIVING (see Format 1). The REMAINDER clause can be used to store the integer remainder of a division operation. The use of the REMAINDER clause is optional and does not alter the results of the original DIVIDE operation. The On Size Error clause applies to both the quotient and the remainder fields. Syntax Definitions: FORMAT 1: DIVIDE {identifier-1 | iteral-1} INTO {identifier-2 [ROUNDED]}… FORMAT 2: DIVIDE {identifier-1 | literal-1} {INTO | BY} {identifier-2 | literal-2} GIVING {identifier-3 [ROUNDED]}... FORMAT 3: DIVIDE {identifier-l/literal-l} {INTO | BY} {identifier-2/literal-2} GIVING {identifier-3 [ROUNDED] [REMAINDER identifier-4]…} [ON SIZE ERROR stmt-group1] [NOT ON SIZE ERROR stmt-group2] [END-DIVIDE] Examples: a=1, b=2, c=4, d=8 Divide b into c d. Divide c into b. Divide c by b giving b. Divide b into 9 giving y remainder z. Arithmetic Syntax