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.

Slides:



Advertisements
Similar presentations
Chapter 6 Structured Data 1. A data structure, or record, in COBOL is a method of combining several variables into one larger variable. – Example: 2.
Advertisements

Introduction to C Programming
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)
C Language Elements (II) H&K Chapter 2 Instructor – Gokcen Cilingir Cpt S 121 (June 22, 2011) Washington State University.
Chapter 2 Basic Elements of Fortan
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
JavaScript, Third Edition
Introduction to C Programming
Computer Science 101 Introduction to Programming.
A First Book of ANSI C Fourth Edition
Lecture 5.
Number Systems Part 2 Numerical Overflow Right and Left Shifts Storage Methods Subtraction Ranges.
Modifications to program Addda.cbl Please use speaker notes for additional information!
1 Please switch off your mobile phones. 2 Data Representation Instructor: Mainak Chaudhuri
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Computer Science 101 Introduction to Programming.
Arithmetic operations and operators, converting data types and formatting programs for output. Year 11 Information Technology.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
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.
Chapter 2: Using Data.
Lecture 31 Numeric Edited Alphabetic (A) AlphaNumeric (X) Numeric (9, V, S) Numeric Edited (9, Z, comma, decimal point, minus sign) –Z = zero suppressed.
The DATA DIVISION Chapter 3. COBOL Data Organization Field - group of characters forming a meaningful unit or basic fact –Characters in a name or digits.
3-1 Chapter 3. To familiarize you with  Ways in which data is organized in COBOL  Rules for forming data-names  Defining input and output files in.
CPS120: Introduction to Computer Science
7-1 Chapter 7.  Basic Arithmetic Verbs  Options Available with Arithmetic Verbs  COMPUTE Statement  Signed Numbers in Arithmetic Operations  Intrinsic.
Formatting, Casts, Special Operators and Round Off Errors 09/18/13.
Chapter 7 File I/O 1. File, Record & Field 2 The file is just a chunk of disk space set aside for data and given a name. The computer has no idea what.
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
Pseudocode. Simple Program Design, Fourth Edition Chapter 2 2 Objectives In this chapter you will be able to: Introduce common words, keywords, and meaningful.
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.
Move and Edited Output1 NON-NUMERIC: Left justify the data in the memory location. If the length of the data is smaller than the length of the field (as.
Arithmetic and Edited Pictures. Arithmetic Verb Template  Most COBOL arithmetic verbs conform to the template above. For example; ADD Takings TO CashTotal.
Chapter 3 P. 1 Data - Variables and Constants Definition A variable is a value which can change during program execution. A constant is a value which can.
Chapter 3 Decision Making. What is IF? The primary method of changing the flow of a program is by making decisions using the IF verb. The following example.
11- 1 Chapter 11.  Avoiding Logic Errors by Validating Input  What to Do If Input Errors Occur  Global Considerations in COBOL  When Data Should Be.
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)
Variables & Function Calls. Overview u Variables  Programmer Defined & Intrinsic  Data Types  Calculation issues u Using Functions  The val() function.
Chapter 3 – Variables and Arithmetic Operations. Variable Rules u Must declare all variable names –List name and type u Keep length to 31 characters –Older.
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.
Any Questions? Agenda Level 77 Initialize Display & Accept Arithmetic Verbs Compute statement String/Unstring Inspect.
Chapter 2 Using Variables and Constant. What is a Constant ? The data in COBOL programs falls in two broad categories: – Constants and Variables A constant.
PowerPoint Presentation: Richard H. Baum, Ph.D. DeVry Institute of Technology 9th Edition Structured COBOL Programming Nancy Stern Hofstra University Robert.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
This is It! It’s been a pleasure! Final Exam – format True / false 5 Multiple choice 5 Short answers10 Data manipulation10 SQL 3 Array’s12 File processing25.
1 Week 5 l Primitive Data types l Assignment l Expressions l Documentation & Style Primitive Types, Assignments, and Expressions.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
11- 1 Chapter 11.  Avoiding Logic Errors by Validating Input  What to Do If Input Errors Occur  Global Considerations in COBOL  When Data Should Be.
Any Questions! 2 Agenda Moving data Arithmetic Verbs Compute statement.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
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.
Chapter 2 Variables.
Revision Lecture
The Selection Structure
Programming in COBOL.
Chapter 3 The DATA DIVISION.
Chapter 2 Variables.
Storing Negative Integers
Computing in COBOL: The Arithmetic Verbs and Intrinsic Functions
THE COMPUTE STATEMENT Purpose: performs mathematical calculations
Data Types and Expressions
Chapter 2 Variables.
Presentation transcript:

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. This is a convenient method of setting variables to start with a known value. Variables are initialized with a VALUE IS clause.

Initializing Variables If you want a variable to have a default value that will be used in the program, you must initialize it in WORKING-STORAGE. A variable that is not initialized has an undefined value until something is moved to it. An undefined value is one that can contain any value.

Initializing Variables For numeric variables, this can become a problem. If you attempt to use the DISPLAY (ADD 1) statement with a numeric variable that contains an undefined value, you probably will produce an error. This does not cause a compiler error, but usually causes an error while the program is running. The program usually aborts with a message such as this: – ATTEMPT TO PERFORM ARITHMETIC WITH NON-NUMERIC DATA OR – VARIABLE THE-NUMBER DOES NOT CONTAIN NUMERIC DATA

Initializing Variables Initializing a variable with a VALUE in WORKING-STORAGE is the same as using MOVE to give it a value. Thereafter, you can use MOVE to assign values to the variable later in the program. Variables that are not initialized contain undefined values until a MOVE moves something to them.

SPACES and ZEROS It is a good practice to initialize variables in WORKING-STORAGE. The usual practice is to initialize – numeric variables to zero – alphanumeric variables to spaces. DO initialize variables in the DATA DIVISION when they are defined, or in the PROCEDURE DIVISION before they are used. DON'T perform any arithmetic functions on an uninitialized numeric variable.

SPACES and ZEROS

Truncate Values

A truncated value occurs when a value that is too large for a numeric variable is moved to the numeric variable, or when a value that is too long for an alphanumeric variable is moved to the alphanumeric variable.

Truncate Values The compiler conveniently fills variables with blanks or zeroes when short or small values are moved to them, or when short or small values are used to initialize them. An alphanumeric variable truncates the right end of the value until the value fits in the variable. A numeric variable truncates the left end of the value until the value fits.

Truncate Values

Multiple move statements

Decimal Number COBOL is a business language, which should be able to deal with decimal numbers, dollars and cents, and percentages. The character in a numeric PICTURE that represents a decimal point is an uppercase V. The following variable holds values ranging from to – E.g. 01 THE-VALUE PIC 999V99.

Decimal Number Any constant values that you move to a decimal variable or use to initialize a decimal variable are written in conventional format, as in these examples:

Decimal Number If you attempt to move a value containing too many decimals, the number is truncated on the right, and some of the decimal information will be lost. In this example, THE- VALUE ends up containing

Decimal Number Truncation still takes place from the high end as well. In this example, THE-VALUE ends up containing because the number is truncated on both the left and the right:

Positive and Negative Numbers COBOL numbers can also contain a positive or negative sign. The PICTURE character for a sign is an initial S. The S must be the first character in the picture.

Displaying Decimal and Sign Numbers that contain an S for a sign (positive or negative) or a V for a decimal are stored in memory in a special format that speeds up calculations. However, this format does not display correctly. The PICTURE character for a sign in a numeric variable that will be used for a DISPLAY is the minus sign (-).

Displaying Decimal and Sign The following variable holds the values through for display purposes: The display sign (-) displays only when the value is negative. If DISPLAY-VALUE contains , it displays as the following:

Displaying Decimal and Sign The - also can be placed at the end of the picture rather than at the beginning. It is fairly common in business programs to see display values specified as follows: – E.g. 01 THE-DISPLAY-VALUE PIC In a display variable, you can suppress the display of leading zeroes, using Z to replace 9 in the picture of the variable. Here is an example: – E.g. 01 THE-DISPLAY-VALUE PIC ZZZZZ9.99-

Editing Characters The minus sign (-), decimal point (.), comma (,) and the character Z are called editing characters. A numeric variable that contains an editing character is called an edited numeric variable. Edited numeric variables should be used only to display values and should not be used in calculations.

COBOL Numeric Operations The COBOL COMPUTE verb is a general- purpose verb that can be used to calculate results. Arithmetic expressions in the COMPUTE verb use the arithmetic operators: + (addition), - (subtraction), *(multiplication), and / (division). You can use parentheses to affect the order in which operations are performed.

COBOL Numeric Operations

The COMPUTE verb has two optional clauses: ROUNDED and ON SIZE ERROR. – ROUNDED rounds the result up or down as necessary, based on the results of the calculation. – The ON SIZE ERROR logic is performed if the result is larger than the variable that is used to store the result. – The statement that follows ON SIZE ERROR also is executed if a COMPUTE statement attempts to dosomething impossible, such as divide by zero.

COBOL Numeric Operations

Quiz 1. What is the value in BIG-NUMBER after the MOVE? 01 BIG-NUMBER PIC 9(5). MOVE 4976 TO BIG-NUMBER 2. What is the value in SMALL-NUMBER after the MOVE? 01 SMALL-NUMBER PIC 9(2). MOVE 4976 TO SMALL-NUMBER. Hint: Numbers are truncated from the left.

Quiz 3. After the following move, THE-VALUE contains Why? 01 THE-VALUE PIC 999V99. MOVE TO THE-VALUE.