Vous êtes sur la page 1sur 19

Understanding COBOL

In a day !!!

2008-09-15
Copyright 2008 TietoEnator Corporation

Veena Katiyar
QA Engineer
TietoEnator Corporation
B&I
veena.katiyar@tietoenator.com
Table of contents.
• Structure of COBOL file .
• Cobol areas .
• Variable declaration .
• Structures .
• Level 77 & level 88 .
• Sentence .
• Paragraph .
• Arrays .
• Simple cobol program .
• RUN STOP .
• Decision making .
• Arithmetic operators .
• Comparision operators .
• PERFORM .
• Complex cobol program .
Copyright 2008 TietoEnator Corporation

• Questions ?

• Thank you !!!

2
Structure of COBOL file .

IDENTIFICATION DIVISION.
* Specify PROGRAM-ID here .
PROGRAM-ID. COBSHL04.

ENVIRONMENT DIVISION.
Define environment here.

DATA DIVISION.
WORKING-STORAGE SECTION.
Define variables here .

PROCEDURE DIVISION.
* Write program here.
Copyright 2008 TietoEnator Corporation
Cobol areas

Sequence no. :- First 6 columns , used to be sequence no. of punched


cards . Nowadays , it’s not required .

Position 7 :- Place an asterisk here then compiler will treat this row as
comment and will not process it .

Area A :- DIVISIONs , paragraphs & SECTIONs must start in this area .


Copyright 2008 TietoEnator Corporation

Area B :- Sentences must start here

4
Variable declaration & Structures
• The WORKING-STORAGE SECTION of the DATA DIVISION is used to create space for
the variables of a program.

• 01 STUDENT-AGE PIC 9(2) .


• 01 MAX-MARKS PIC 999 VALUE 100 .
• 01 STUDENT-NAME PIC X(10) VALUE SPACES.
ACCEPT STUDENT-AGE .
DISPLAY STUDENT-AGE .

• 01 STUDENT-REC .
05 FILLER PIC X(5) VALUE “Age” .
05 STUDENT-AGE PIC 9(2) VALUE ZERO.
05 FILLER PIC X(5) VALUE “Marks”.
05 MAX-MARKS PIC 999 VALUE 100 .
05 FILLER PIC x(5) VALUE “Name” .
05 STUDENT-NAME PIC X(10) VALUE SPACES .
Copyright 2008 TietoEnator Corporation

ACCEPT STUDENT-AGE .
DISPLAY STUDENT-REC .

5
Level 77 & Level 88
• Level 77 :- A variable that is not a structure can be given a level number of 77
instead of 01 .
77 YES-NO PIC X.
• A level number of 77 indicates to the compiler that the variable named after the
77 is a simple elementary variable and not a structure.

• Level 88 :- It is a special level number used to improve the readability of COBOL


programs and to improve IF tests.
• It does not have a PICTURE, but it does have a value. A level 88 is always
associated with another variable and is a condition name for that variable. Here
is an example:

01 YES-NO PIC X.
88 ANSWER-IS-YES VALUE "Y".

MOVE “Y” TO YES-NO .


IF ANSWER-IS-YES
Copyright 2008 TietoEnator Corporation

* if YES-NO = “Y”
DISPLAY “Value of YES-NO is Y” .

6
Sentence & Paragraph

• Paragraph :- starts in area A (8-11) . It’s a group of sentences .

MAIN-PROG .
PERFORM GET-NAME .
PERFORM GET-AGE .
STOP RUN .

GET-NAME .
DISPLAY “Good Morning !!!”.
DISPLAY “Enter your name”.
ACCEPT USER-NAME .

GET-AGE .
DISPLAY “Enter your age” .
Copyright 2008 TietoEnator Corporation

ACCEPT USER-AGE .

• Sentence :- starts in area B (12 onwards) .

7
Arrays

Arrays are used to handle multiple records of same structure type .


01 STUDENT-RECORD .
02 MYTABLE OCCURS 2 TIMES .
05 ST-NAME PIC XXXXX.
05 FILLER PIC X(10) VALUE SPACES.
05 ST-AGE PIC 99.

PROCEDURE DIVISION .
DISPLAY "Enter Student name [1] : ".
ACCEPT ST-NAME(1) .
DISPLAY "Enter Student age [1] : " .
ACCEPT ST-AGE(1) .
Copyright 2008 TietoEnator Corporation

DISPLAY MYTABLE(1)

8
Simple COBOL Program

IDENTIFICATION DIVISION
PROGRAM-ID. MYFIRST .
ENVIRONMENT DIVISION
DATA DIVISION
WORKING-STORAGE SECTION.
01 USER-NAME PIC X(5).
PROCEDURE SECTION .

MAIN-PROG .
PERFORM GET-NAME .
STOP RUN .

GET-NAME .
DISPLAY “Enter your name”.
ACCEPT USER-NAME .
Copyright 2008 TietoEnator Corporation

DISPLAY “Welcome “ USER-NAME .

9
RUN STOP
01 MYNO PIC 9.
88 MYNO-ODD VALUES 1 3 5 7 9.
88 MYNO-EVEN VALUES 2 4 6 8 .
88 MYNO-ZERO VALUE 0.

PROCEDURE DIVISION.

DISPLAY "Enter any no. between 0 to 9 ".


ACCEPT MYNO .

IF MYNO-ODD
DISPLAY "It's a odd no."
STOP RUN .
IF MYNO-EVEN
DISPLAY "It's a even no.“
STOP RUN .
Copyright 2008 TietoEnator Corporation

IF MYNO-ZERO
DISPLAY "It's zero“
STOP RUN .
STOP RUN ends the execution at the end of the paragraph .
10
Decision making

• IF :-
DISPLAY “Enter Y or N”.
ACCEPT YES-NO .

IF YES-NO = “Y”
DISPLAY ”You have entered Y”.
IF YES-NO = “N”
DISPLAY ”You have entered N”.

• IF-ELSE :-
IF YES-NO = “Y”
DISPLAY “You have entered Y”
Copyright 2008 TietoEnator Corporation

ELSE
DISPLAY “You have not entered Y” .

11
Arithmetic operations
• Addition :-
COMPUTE SALARY = SALARY + 1000 .
ADD 1000 TO SALARY .
ADD 1000 TO 5000 GIVING SALARY .
* SALARY = 1000 + 5000

• Subtraction :-
COMPUTE SALARY = SALARY – 1000.
SUBTRACT 1000 FROM SALARY .
SUBTRACT 1000 FROM 5000 GIVING SALARY .
* SALARY = 5000 – 1000 .

• Multiplication :-
COMPUTE SALARY = SALARY * 1000 .
MULTIPLY 1000 BY SALARY .
* SALARY = SALARY * 1000 .
MULTIPLY 1000 BY 10 GIVING SALARY .
* Salary = 1000 * 10 .
Copyright 2008 TietoEnator Corporation

• Division :-
COMPUTE SALARY = SALARY / 1000 .
DIVIDE 10 INTO SALARY.
DIVIDE 17 INTO 1000 GIVING SALARY REMAINDER MY-REMAINDER .
*SALARY = 1000 / 17 .
* Remainder in MY-REMAINDER variable .
12
13
Copyright 2008 TietoEnator Corporation

Comparision Operators
PERFORM
• Paragraphs are similar to functions .
• PERFORM execute paragraphs .

PROCEDURE DIVISION .
MOVE 10 TO A B .

PERFORM ADD-IT 10 TIMES .


STOP RUN .

MOVE 10 TO A B .
PERFORM ADD-IT UNTIL B >100 .
STOP RUN .

PERFORM ADD-IT VARYING A FROM 1 BY 5 UNTIL B > 100 .

ADD-IT .
Copyright 2008 TietoEnator Corporation

COMPUTE B = A + B .
DISPLAY B.

14
Complex COBOL Program
IDENTIFICATION DIVISION .
PROGRAM-ID . compprog .
ENVIRONMENT DIVISION .

DATA DIVISION .
WORKING-STORAGE SECTION .
77 UCASE PIC X(26) VALUE "ABCDEFGHIJKLMNOPQRSTUVWXYZ".
77 LCASE PIC X(26) VALUE "abcdefghijklmnopqrstuvwxyz".
01 UTEXT PIC X(20) VALUE SPACES .
01 A-SPACE PIC X(5) VALUE SPACES .
01 CHOICE PIC 9 VALUE ZERO .
01 YESNO PIC X VALUE "Y".

PROCEDURE DIVISION .
PERFORM MAIN-PROC UNTIL YESNO = "N".
STOP RUN .
Copyright 2008 TietoEnator Corporation

MAIN-PROC .
PERFORM GET-DATA .
PERFORM GET-CASE.
15
PERFORM CHANGE-CASE.
PERFORM DISPLAY-DATA .
PERFORM ASK-ANOTHER .

GET-DATA .
DISPLAY A-SPACE .
DISPLAY "Enter text string > ".
ACCEPT UTEXT .
DISPLAY A-SPACE .

GET-CASE .
DISPLAY A-SPACE .
DISPLAY "Enter 1 to change to uppercase ." .
DISPLAY "Enter 2 to change to lowercase ." .
ACCEPT CHOICE .

CHANGE-CASE .
Copyright 2008 TietoEnator Corporation

IF CHOICE = 1
INSPECT UTEXT CONVERTING LCASE TO UCASE
ELSE
INSPECT UTEXT CONVERTING UCASE TO LCASE .

16
DISPLAY-DATA .
DISPLAY A-SPACE .
DISPLAY "Result :-".
DISPLAY A-SPACE .
DISPLAY UTEXT

ASK-ANOTHER .
DISPLAY A-SPACE .

DISPLAY "Change another text string [Y/N] ? > ".


ACCEPT YESNO .

IF YESNO = "Y" OR YESNO = "y"


MOVE "Y" TO YESNO
ELSE
Copyright 2008 TietoEnator Corporation

MOVE "N" TO YESNO .

17
18
Copyright 2008 TietoEnator Corporation

QUESTIONS ???
Thank you.
Copyright 2008 TietoEnator Corporation

Veena Katiyar
QA Engineer
TietoEnator Corporation
B&I
veena.katiyar@tietoenator.com

Vous aimerez peut-être aussi