Upload
qajiny
View
1.878
Download
2
Embed Size (px)
DESCRIPTION
Learn COBOL in an easy way !!!
Citation preview
Cop
yri
gh
t 2
00
8 T
ieto
En
ato
r C
orp
ora
tion
Understanding COBOL In a day !!!
2008-09-15
Veena KatiyarQA Engineer
TietoEnator CorporationB & I
2
Cop
yri
gh
t 2
00
8 T
ieto
En
ato
r C
orp
ora
tion
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 .
• Questions ?
• Thank you !!!
Cop
yri
gh
t 2
00
8 T
ieto
En
ato
r C
orp
ora
tion
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.
4
Cop
yri
gh
t 2
00
8 T
ieto
En
ato
r C
orp
ora
tion
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 .
Area B :- Sentences must start here
5
Cop
yri
gh
t 2
00
8 T
ieto
En
ato
r C
orp
ora
tion
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 .
ACCEPT STUDENT-AGE .DISPLAY STUDENT-REC .
6
Cop
yri
gh
t 2
00
8 T
ieto
En
ato
r C
orp
ora
tion
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* if YES-NO = “Y” DISPLAY “Value of YES-NO is Y” .
7
Cop
yri
gh
t 2
00
8 T
ieto
En
ato
r C
orp
ora
tion
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” .ACCEPT USER-AGE .
• Sentence :- starts in area B (12 onwards) .
8
Cop
yri
gh
t 2
00
8 T
ieto
En
ato
r C
orp
ora
tion
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) .
DISPLAY MYTABLE(1)
9
Cop
yri
gh
t 2
00
8 T
ieto
En
ato
r C
orp
ora
tion
Simple COBOL Program
IDENTIFICATION DIVISIONPROGRAM-ID. MYFIRST .ENVIRONMENT DIVISIONDATA DIVISIONWORKING-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 .DISPLAY “Welcome “ USER-NAME .
10
Cop
yri
gh
t 2
00
8 T
ieto
En
ato
r C
orp
ora
tion
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 .IF MYNO-ZERO DISPLAY "It's zero“ STOP RUN .STOP RUN ends the execution at the end of the paragraph .
11
Cop
yri
gh
t 2
00
8 T
ieto
En
ato
r C
orp
ora
tion
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”ELSE
DISPLAY “You have not entered Y” .
12
Cop
yri
gh
t 2
00
8 T
ieto
En
ato
r C
orp
ora
tion
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 .
• 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 .
13
Cop
yri
gh
t 2
00
8 T
ieto
En
ato
r C
orp
ora
tion
Comparision Operators
14
Cop
yri
gh
t 2
00
8 T
ieto
En
ato
r C
orp
ora
tion
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 .COMPUTE B = A + B .
DISPLAY B.
15
Cop
yri
gh
t 2
00
8 T
ieto
En
ato
r C
orp
ora
tion
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 .
MAIN-PROC . PERFORM GET-DATA . PERFORM GET-CASE.
16
Cop
yri
gh
t 2
00
8 T
ieto
En
ato
r C
orp
ora
tion
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 . IF CHOICE = 1
INSPECT UTEXT CONVERTING LCASE TO UCASE ELSE
INSPECT UTEXT CONVERTING UCASE TO LCASE .
17
Cop
yri
gh
t 2
00
8 T
ieto
En
ato
r C
orp
ora
tion
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 MOVE "N" TO YESNO .
Cop
yri
gh
t 2
00
8 T
ieto
En
ato
r C
orp
ora
tion
18
QUESTIONS ???
Cop
yri
gh
t 2
00
8 T
ieto
En
ato
r C
orp
ora
tion
Thank you.
Veena KatiyarQA EngineerTietoEnator CorporationB & [email protected]