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.

Slides:



Advertisements
Similar presentations
Introducing JavaScript
Advertisements

VARIABLES AND DEBUGGING Beginning Programming. Assignment Statements  Used to hold values in a variable  Calculates a result and stores it in a variable.
2-1 Chapter 2.  Coding Requirements of IDENTIFICATION DIVISION  Sections of ENVIRONMENT DIVISION  Assigning Files to Devices in ENVIRONMENT DIVISION.
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.
3-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emertius)
The IDENTIFICATION and ENVIRONMENT DIVISIONS Chapter 2.
1 ICS103 Programming in C Lecture 3: Introduction to C (2)
Understanding Arrays and Pointers Object-Oriented Programming Using C++ Second Edition 3.
Data types and variables
Chapter 2 Data Types, Declarations, and Displays
Chapter 2: Introduction to C++.
XP Tutorial 1 New Perspectives on JavaScript, Comprehensive1 Introducing JavaScript Hiding Addresses from Spammers.
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
String Escape Sequences
CH1 – A 1 st Program Using C#. Program Set of instructions which tell a computer what to do. Machine Language Basic language computers use to control.
Objectives You should be able to describe: Data Types
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
1 Lab Session-III CSIT-120 Fall 2000 Revising Previous session Data input and output While loop Exercise Limits and Bounds Session III-B (starts on slide.
COBOL Basics 1. COBOL coding rules  Almost all COBOL compilers treat a line of COBOL code as if it contained two distinct areas. These are known as;
Programming Logic and Design Sixth Edition Chapter 2 Working with Data, Creating Modules, and Designing High-Quality Programs.
Chapter To familiarize you with  Why COBOL is a popular business-oriented language.  Programming practices and techniques  History of COBOL.
3-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emertius)
Introduction to COBOL. COBOL  COBOL is an acronym which stands for Common Business Oriented Language.  The name indicates the target area of COBOL applications.
Modifications to program Addda.cbl Please use speaker notes for additional information!
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Chapter 2 Overview of C Part I J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
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.
1 JavaScript in Context. Server-Side Programming.
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.
3-1 The DATA DIVISION Chapter Chapter Objectives To familiarize you with Systems design considerations Ways in which data is organized Rules for.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
CPS120: Introduction to Computer Science
Chapter 8 High-Level Programming Languages. 8-2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Programming Logic and Design Sixth Edition Chapter 5 Looping.
Introduction to programming in the Java programming language.
Course Title: Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 03 Conditional statement 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER)
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
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.
Introduction to Programming
Structured Programming
Structured COBOL Programming, Stern & Stern, 9th Edition CHAPTER 2 Cobol Language Fundamentals.
2-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)
Chapter 4 Using PERFORM, GO TO, and IF to Control Programs.
IBM-Mainframes COBOL Class-1. Background and History  COBOL is an acronym for: Common Business Oriented Language  COBOL was developed in 1959 by the.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
Tutorial 3: Using Variables and Constants1 Tutorial 3 Using Variables and Constants.
CPS120: Introduction to Computer Science Variables and Constants.
Analysis of SAMPLE1.CBL Please check speaker notes for additional information!
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
1 C Syntax and Semantics Dr. Sherif Mohamed Tawfik Lecture Two.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
Chapter 2: Introduction to C++
Structured Programming
ICS103 Programming in C Lecture 3: Introduction to C (2)
Programming in COBOL.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 2 Applications and Data.
Designing and Debugging Batch and Interactive COBOL Programs
Chapter 3 The DATA DIVISION.
An Introduction to Structured Program Design in COBOL
Chapter 2: Introduction to C++.
Programming in COBOL.
Presentation transcript:

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 is a value that cannot be modified while the program is running. Character constants are enclosed in quotation marks. Numeric constants are not.

What is a Constant ? IDENTIFICATION DIVISION. PROGRAM-ID. CONST. ENVIRONMENT DIVISION. DATA DIVISION. PROCEDURE DIVISION. PROGRAM-BEGIN. DISPLAY "Hello world". DISPLAY 55. String Constant Numeric Constant

What is a variable? A variable is a value that can be changed while the program is running. When a variable is created in a program, an area of memory is set aside to hold values. A variable is given a name. The name can be used in the program to refer to the value. The value stored in memory can be modified while the program is running by using the variable name.

Defining Numeric Variables in COBOL Variable names use the same characters as paragraph names: – A through Z, – 0 through 9, – hyphen (-). – E.g. 01THE-NUMBER PICTURE 9999.

Defining Numeric Variables in COBOL A COBOL variable definition contains at least three parts: – The level number – The name – The PICTURE e.g. 01THE-NUMBER PICTURE 9999.

Defining Numeric Variables in COBOL In the syntax, the level number is 01. The level number 01 must be in Area A, columns 8 through 11. Variable definition is the name of the variable and, in this case, is THE-NUMBER. This is the Data name used to identify the variable. – The variable will be referred to by its data name, THE- NUMBER The name of the variable must start in Area B, columns 12 through 72. – In this example, THE-NUMBER starts in column 12.

Defining Numeric Variables in COBOL The PICTURE defines two things about a variable: – the size of the variable (the number of bytes used in memory for the value) – the type of data that can be stored in the variable. The picture 9999 indicates that four numeric characters can be stored in the variable named THE-NUMBER.

Defining Numeric Variables in COBOL The 9999 in the picture does not indicate that the variable contains the value It indicates that the variable can be used for numeric values in the range 0 through 9,999. – For example – The values 17 and 6,489 will both fit in THE- NUMBER, but the value 65,413 is too large.

Defining Numeric Variables in COBOL IDENTIFICATION DIVISION. PROGRAM-ID. ADD01. DATA DIVISION. WORKING-STORAGE SECTION. 01 FIRST-NUMBERPICTURE SECOND-NUMBER PICTURE THE-RESULT PICTURE 999. PROCEDURE DIVISION. DISPLAY "Enter the first number.". ACCEPT FIRST-NUMBER. DISPLAY "Enter the second number.". ACCEPT SECOND-NUMBER. COMPUTE THE-RESULT = FIRST-NUMBER + SECOND-NUMBER. DISPLAY "The result is:". DISPLAY THE-RESULT. DISPLAY “The result is: “ THE-RESULT. STOP RUN.

Naming Variables in COBOL COBOL variable names are similar to paragraph and section names because they can use any of the uppercase alphabet characters. The digits 0 through 9, and the hyphen (but not as a starting character). COBOL variable and paragraph names are limited to 30 characters.

Naming Variables in COBOL Valid NameInvalid NameExplanation of Invalid Name TOTAL-DOLLARSTOTAL-$ Uses an invalid $ in the name SUM-OF-COLUMNSsum-of-columns Uses lowercase letters 7-BY-57_BY_5 Uses the invalid _ character in the name MINUS-RESULT-RESULT Starts with a hyphen BOEING-707-SEATSBOEING-707-MAXIMUM- SEATING-CAPACITY Exceeds 30 characters

Defining and Using Variables A numeric variable is used to store numbers. – Example 01 THE-NUMBER PICTURE IS 99. Variables that can hold character data are called alphanumeric variables. – Example 01 THE-MESSAGE PICTURE IS XXXXXXXXXX.

Defining and Using Variables An alphanumeric variable can also be used to hold numbers (such as storing 123 in a PICTURE IS XXX variable) But will not be able to use the values as numbers. – For example, you could display the PICTURE IS XXX variable containing 123, but you couldn't use the COMPUTE verb to add 1 to it.

Defining and Using Variables IDENTIFICATION DIVISION. PROGRAM-ID. HELLO02. DATA DIVISION. WORKING-STORAGE SECTION. 01 THE-NAME PICTURE XXXXXXXXXX. PROCEDURE DIVISION. DISPLAY "Enter someone's name.". ACCEPT THE-NAME. DISPLAY "Hello " THE-NAME. STOP RUN.

Introducing the MOVE Verb The MOVE verb in COBOL is a general-purpose verb, used to store a value in a variable. – Syntax: MOVE value TO variable. In this syntax, variable must be a variable defined in the DATA DIVISION, and value can be another variable or a constant.

Introducing the MOVE Verb Here are some examples: MOVE 12 TO THE-NUMBER. MOVE ONE-NUMBER TO ANOTHER-NUMBER. MOVE "XYZ" TO THE-MESSAGE. MOVE is used to set a variable to a specific value. – For example, – if you're going to use the variable THE-COUNTER as a counter and you need the count to start at 1, you might use the following as one method of setting up the variable with a starting value: MOVE 1 TO THE-COUNTER.

Introducing the MOVE Verb It copies values from the source variable and stores them in the target variable. CommandEffect MOVE 19 TO THE-NUMBERStores 19 in the variable THE-NUMBER, or sets THE- NUMBER to a value of 19 MOVE "Hello" TO THE-MESSAGEStores Hello in the variable THE-MESSAGE, or sets THE-MESSAGE to contain Hello MOVE A-NUMBER TO THE-NUMBERLocates the variable named A-NUMBER, gets the value stored there, and copies it or moves it to the variable named THE- NUMBER MOVE THE-OLD-NAME TO THE-NEW- NAME Locates the variable named THE-OLD-NAME, gets the value stored there, and copies it or moves it to the variable named THE-NEW-NAME

Extra Reading Read and test the programs – Formatting output – Program 2.8 – Program 2.9 – Layout and Punctuation – Program 2.10 – Continuation Characters

Exercises 1.How many bytes of memory are used by the following variable? 01 CUSTOMER-NAME PIC X(30). 2.What type of data can be stored in CUSTOMER-NAME? 3.If you move a value to CUSTOMER-NAME, such as MOVE "ABC Company" TO CUSTOMER-NAME. only the first 11 characters of the variable are filled with the value. What is placed in the remaining 19 character Positions?

Exercises 4.What is the largest number that can be moved using MOVE to the following variable? 01 UNITS-SOLD PIC 9(4). 4.What is the smallest value that can be moved using MOVE to UNITS-SOLD? 5.If 12 is moved to UNITS-SOLD, as in MOVE 12 to UNITS-SOLD. what values are stored in the four numeric positions of UNITS-SOLD?

Paragraph Names Paragraph names are used only as bookmarks, it is possible to insert more paragraph names into a program. Remember that you can assign your own paragraph names. The rules for naming paragraphs are similar to the rules for naming variables DO use uppercase paragraph names if you want your code to be portable.

Paragraph Names Paragraph Naming Rules: – A paragraph name can contain 30 characters. In fact, a paragraph name can be longer than 30 characters, but the compiler will warn you that it will use only the first 30 characters of the name. The remaining characters can be included in the name but the compiler will ignore them. – The characters can be A through Z, 0 through 9, and the hyphen (-). Some compilers also allow the lowercase characters a through z. – The paragraph name must not start with the hyphen. – Paragraph names must start in Area A, columns 8 through 11, and must end with a period.

Paragraph Names IDENTIFICATION DIVISION. PROGRAM-ID. ADD04. DATA DIVISION. WORKING-STORAGE SECTION. 01 FIRST-NUMBER PIC SECOND-NUMBER PIC THE-RESULT PIC 999. PROCEDURE DIVISION. PROGRAM-BEGIN. DISPLAY "This program will add 2 numbers.". GET-FIRST-NUMBER. DISPLAY "Enter the first number.". ACCEPT FIRST-NUMBER. GET-SECOND-NUMBER. DISPLAY "Enter the second number.". DISPLAY "The result is " THE-RESULT. PROGRAM-DONE. STOP RUN.

Reserved Words? Reserved words are reserved in the language to have a special meaning, and the programmer cannot use these words for some other purpose. PROGRAM-ID. DISPLAY. DO name programs, variables, and paragraphs with descriptive names that make their use obvious. DON'T name programs, variables, or paragraphs with reserved words.

What is STOP RUN? STOP RUN can occur anywhere in the program It will stop the execution. In all the examples so far, the STOP RUN is placed in its own separate paragraph to make it stand out as the end of the program

What Is the PERFORM Verb? Suppose you had one action that you performed several times in a program. In top- to-bottom execution, you would have to code that same logic over and over. The PERFORM verb avoids this problem of coding repetitive actions.

What Is the PERFORM Verb? IDENTIFICATION DIVISION. PROGRAM-ID. HELLO04. * This program illustrates the use of a PERFORM DATA DIVISION. PROCEDURE DIVISION. PROGRAM-BEGIN. DISPLAY "Today's message is:". PERFORM SAY-HELLO. PROGRAM-DONE. STOP RUN. SAY-HELLO. DISPLAY "Hello world". PERFORM SAY-HELLO indicates the following: 1.Locate the paragraph named SAY-HELLO. 2.Jump to that paragraph and start executing there. 3.When that paragraph ends, return to the end of this sentence (PERFORM SAY- HELLO).

What Is the PERFORM Verb? – The Incorrect way IDENTIFICATION DIVISION. PROGRAM-ID. HELLO05. * This program illustrates the incorrect placement of a * Paragraph that is the target of a perform DATA DIVISION. PROCEDURE DIVISION. PROGRAM-BEGIN. DISPLAY "Today's message is:". PERFORM SAY-HELLO. SAY-HELLO. DISPLAY "Hello world". PROGRAM-DONE. STOP RUN.

What Is the PERFORM Verb? A PERFORM serves to break up a program into smaller, more manageable pieces. If you're changing the sales commission from 10 percent to 11 percent, it's much easier to search through a long program looking for a paragraph named CALCULATE-COMMISSION than to plow through a long list of code not broken into paragraphs.

What Is the PERFORM Verb? DO locate repetitive actions in your programs, and create separate paragraphs containing those actions. Then PERFORM the paragraph wherever those actions are needed. DON'T keep typing the same code over and over in one program.

Exercise 1.If the code in a paragraph is designed to locate overdue customers, which of the following would be the best name for the paragraph? a)LOCATE-CUSTOMERS. b)FIND-SOME-STUFF. c)LOCATE-OVERDUE-CUSTOMERS.

Reading Read and try the sample program at chapter 3