COBOL学习笔记_20121231
- 格式:docx
- 大小:82.70 KB
- 文档页数:18
过程部之三——执行语句(PERFORM语句)一、执行语句的作用在一个COBOL程序中过程部中往往有一部分语句是需要多次执行的。
这时需要使用执行语句。
二、执行语句最基本的形式PERFORM语句的最基本的形式:PERFORM 过程名过程部中的节名、段名称为过程名。
用PERFORM语句只能转到指定的节或段的开头,执行完该节(段)的全部语句后返回。
有时,需要多次执行的不是一个段(节),而是若干个端(节),则应指明从哪一段(节)起到哪一段(节)止。
则PERFORM语句的一般格式可扩充为THROUGHPERFORM 过程名1 过程名2THRU三、执行语句的使用规则1.PERFORM语句的嵌套嵌套不能交叉,后一个被“调用”的语句序列或者全部套在前一个被“调用”的语句序列之中,或者全部在它之外是正确的。
不允许二者交叉或者有共同的终点2.在PERFORM语句中所执行的语句序列中,可以含有转移语句,可以使流程转到语句序列之外,但一般应转回到此语句序列,以便最后能执行此语句序列的最后一个句子。
例: T1. PERFORM T3 THRU T5T2. …T3 …T4 GO TO T6T5 …T6 …T7 GO TO T5执行顺序:T1 => T3 => T4 => T6 => T7 => T5 => T2 => T3 => T4 => T6 => T7 => T5 => T6 => T7 => T5 ……3.使用PERFORM语句实现循环这种形式的PERFORM语句的一般形式为THROUGH 整数PERFORM 过程名1 过程名2 TIMESTHRU 标识符说明:(1)标识符应为整数数据型。
(2)如果此标识符的值在执行语句序列中有变化,不会影响执行的次数。
即以它开始时的值决定执行的次数例:PERFORM T1 THRU T5 N TIMES.四、执行语句的较复杂的形式PERFORM语句还有以下两种比较复杂的形式1.反复执行指定的语句序列,直到给定的条件满足为止。
英文缩写: COBOL (Common business Oriented Language)中文译名: COBOL语言解释: 一种适合于商业及数据处理的类似英语的程序设计语言。
这种语言可使商业数据处理过程精确表达。
COBOL(面向商业的通用语言,又称为企业管理语言、数据处理语言等,Common Business Oriented Langauge)是最早的高级编程语言之一,是世界上第一个商用语言。
1 COBOL的历史1959年5月,五角大楼委托格雷斯·霍波(G.Hopper)博士领导一个委员会并由Rear Admiral Grace Hopper公司主持开发,并于1961年由美国数据系统语言协会公布。
正式发布于1960年4月,称为Cobol-60。
1959年12月出现了第一个COBOL语言文本,并于1960年4月正式发表,因此也被称为COBOL-60。
1968年美国国家标准委员会ANSI 又通过了新的标准ANSI COBOL X.3.23-1968。
1972年国际标准化组织ISO将其作为ISO COBOL-72。
1974年ANSI对COBOL-68作出了修订扩充,形成了ANSI COBOL X3.23-1974,随后1978年ISO采用其形成ISO COBOL-78,后来又提出了ANSI X3.23-85,增加了很多结构化程序设计的内容,ISO标准ISO1989:1985也采用了该标准。
COBOL 标准还在不断地改进和完善,最新的标准COBOL 2002主体已经于2002年12月出版,其他部分还在不断地完善之中。
现在最新的版本是Cobol-2002。
1963年,美国国家标准研究所(ANSI)进行了标准化,但是ANSI标准很少被遵循;因此,COBOL程序只是部分可移植的。
2 COBOL的重要性经过40多年的不断修改、丰富完善和标准化,COBOL已发展为多种版本的庞大语言,在财会工作、统计报表、计划编制、情报检索、人事管理等数据管理及商业数据处理领域,都有着广泛的应用。
第1章 COBOL简介COBOL是Common Business Oriented Language的缩写,即公用面向商业语言的意思。
在系统地学习COBOL程序设计语言之前,有必要首先对COBOL有一个大致的了解。
本章将分别从背景知识、语法格式、以及实际创建COBOL程序3个方面对其进行简要介绍。
1.1 背景知识在介绍COBOL之前,首先需要了解一下大型机的概念。
大型机也叫主机、大机、mainframe等,属于一种商用高端服务器。
目前大型机主要由IBM公司所生产,最新型号为z系列大型机。
COBOL便是用于大型机上应用软件开发的最主要的程序设计语言。
大型机上的工作大体上可以分为两类,分别为系统方向和开发方向。
系统方向的分工很多,包括使用Tivoli监控系统;使用RACF进行安全管理;使用SMS进行存储管理等等。
开发方向的市场需求相对系统方向则要大得多。
对于开发方向,主要需要掌握以下知识。
❑COBOL:最主要的程序设计语言。
❑JCL:作业控制语言,调用由COBOL编写的程序。
❑VSAM:程序中通常用到的文件类型。
❑DB2:大型机上最主要的数据库,通常以COBOL语言为宿主语言。
❑CICS:用于联机交易的中间件,通常也是以COBOL语言为宿主语言。
对于以上知识,在本书中都有详细介绍。
此外,对于大型机底层的开发,还需要掌握大型机上的汇编语言。
对于大型机汇编语言,在本书中也有具体讲解。
COBOL作为大型机上最主要的程序设计语言,迄今已有40多年的历史。
然而,经过40多年的沧桑,COBOL不仅没有被淘汰,反而在当今愈加流行起来。
关于这一点,主要应该是由以下3个因素所决定的。
❑COBOL广泛应用于广泛应用于银行业、信用卡业、保险业、制造业、航空业等等。
这些领域对于稳定性的要求都是十分高的,因此从很大程度上也保证了COBOL的不可取代性。
❑COBOL自身也在不断发展和更新中。
例如,COBOL本身主要属于面向过程的语言。
COBOL语言的发展概述• COBOL是Common Business Oriented Language(商业通用语言)的缩写。
• COBOL不仅是商业数据处理的理想语言,而且广泛应用于数据管理领域,因此COBOL也被称为“用于管理的语言”。
• 在计算机的应用领域中,数据处理(data processing)是应用最为广泛的一个领域。
数据处理的日益广泛应用要求人们设计出能满足实际数据处理工作中各种要求的一种计算机语言。
COBOL就是在这种形势下应运而生的。
COBOL语言的特点• 最适于数据处理领域。
• COBOL比较接近于自然语言(英语)。
• 通用性强。
• COBOL的结构严谨,层次分明。
• COBOL的缺点是比较繁琐。
COBOL程序的结构• COBOL源程序的结构• 标识部 环境部 数据部 过程部段 段 段 节 节 节 节 节 节 节 节 节 段 …… 描述体 …… 段 ……句子 …… 句子 …… 语句 ……动词 ……COBOL的四部标识部(IDENTIFICATION DIVISION)主要用来指定源程序名字,也可以写入其它用作备忘的某些信息(如日期、作者等)。
环境部 (ENVIRONMENT DIVISION)指出程序中用到的数据文件名与计算机系统的设备的对应关系,即把某一文件名与某一外部设备联系起来。
数据部(DATA DIVISION)程序中所用到的全部变量都应在数据部中说明它们的类型和所占的内存。
过程部(PROCEDURE DIVISION)编写相应的程序语句来实现源程序需要实现的相应功能。
COBOL源程序的书写格式• ANSI书写格式标号区续行区 A 区 B 区(正文区)注释1 6 7 8 11 12 72 73 80• 终端格式• COBOL字符和COBOL字• COBOL字符 COBOL字• 系统字符集保留字• COBOL字符集例如:DIVISION,SECTION • 包括以下字符:用户字(非保留字)・数字:0〜9・大写字母:A〜Z・小写字母:a〜z・ 专用字符: + ,-,*,/等数据名的命名规则• 每个数据名的长度为1〜30个字符之间。
AHEADETP MF4组1班COBOL上机实验报告姓名:编号:CN0001日期: 2011.02.XXListCOBOL Exercise (3)1. HELLO (3)2. ACCEPT1 (5)3. READ1 (12)4. WRITE (14)5. SIGN1 (21)6. STRING (25)7. DATE1 (29)8. PARM1 (32)9. COPY1 (36)10. SUBPROGRAM (38)11. TABLE1 (45)12. SEARCH1 (52)13. SORT1 (54)14. SEQ1000 (57)15. SEQ2000 (60)16. RPT1000 (63)COBOL Exercise1.HELLO这是一个入门程序,功能是显示:“HELLO WORLD”创建CN0001.COBLE.LOADLIB 的PDS数据集文件:COBOL语言编译的HELLO源程序代码,源代码如图:文件名为COMPILE的JCL文件源代码如图:文件名为GOHELLO的JCL文件源代码如图:2.ACCEPT1这个程序主要是学习ACCEPT语句,该语句主要用于接收数据,当用户从终端上输入数据时,这些数据将通过ACCEPT语句保存到相应变量中。
文件名为ACCEPT1的COBOL文件源代码如图:运行后结果如图:运行文件名为GACCEPT1的JCL文件源代码如图:运行结果如图:COMPUTE1和COMPUTE2利用ACCEPT语句接受数据,然后进行简单的运算。
COMPUTE1:COBOL代码如图:把文件名为COMPILE 的JCL文件SUB,使相应的COBOL变为可执行文件,运行后如图:编写Gcompute1的JCL源程序代码文件,它可调用上步生成的可执行文件,源代码如图:运行后结果如下图:COBOL代码如图:把文件名为COMPILE 的JCL文件SUB,使相应的COBOL变为可执行文件,运行后如图:编写文件名为Gcompute2的JCL源程序代码文件,它可调用上步生成的可执行文件,源代码如图:对接收的值N.的一系列运算过程参照相应的COBOL程序,如下图:运行后值=【(12+3)/5 – 2 】* 8=08,下图为运行结果:3.READ1这个程序主要是学习READ语句,该语句用于从文件中读取数据。
COBOL指针用法什么是COBOL指针?COBOL(Common Business-Oriented Language)是一种用于商业应用程序开发的编程语言。
COBOL指针是COBOL语言中的一种特殊数据类型,用于存储和操作内存地址。
在COBOL中,指针允许程序员直接访问和操作内存中的数据,而不是通过变量名来引用数据。
指针可以指向其他变量、数据结构或程序段,使得程序具有更高的灵活性和效率。
COBOL指针的声明和初始化在COBOL中,可以使用POINTER关键字来声明指针变量。
指针变量必须在WORKING-STORAGE SECTION或LINKAGE SECTION中声明。
以下是一个COBOL指针的声明和初始化的示例:WORKING-STORAGE SECTION.01 MY-POINTER POINTER.05 POINTER-VALUE USAGE IS POINTER.在上面的示例中,我们声明了一个名为MY-POINTER的指针变量,并使用POINTER-VALUE作为其实际存储地址的字段。
要初始化指针变量,可以使用SET语句将一个有效的地址赋给指针变量,如下所示:SET ADDRESS OF MY-DATA TO MY-POINTER上述代码将MY-DATA的地址赋给了MY-POINTER指针变量。
COBOL指针的使用COBOL指针可以用于多种情况,包括:1. 动态内存分配COBOL指针可以用于动态内存分配,允许程序在运行时根据需要分配和释放内存。
这对于处理可变长度的数据非常有用。
以下是一个使用COBOL指针进行动态内存分配的示例:WORKING-STORAGE SECTION.01 DYNAMIC-DATA POINTER.05 DATA-LENGTH PIC S9(4) COMP.05 DATA-ADDRESS USAGE IS POINTER.PROCEDURE DIVISION.MOVE 100 TO DATA-LENGTHSET ADDRESS OF DYNAMIC-DATA TO DATA-ADDRESSCALL "CBL_MALLOC" USING DATA-LENGTHDISPLAY "Allocated memory address: " DATA-ADDRESS.在上面的示例中,我们使用CBL_MALLOC函数分配了一个长度为100的动态内存,并将其地址存储在DYNAMIC-DATA指针变量中。
COB培训教程COB培训教程是一种非常实用的IT培训方案,它可以帮助初学者迅速入门COBOL程序开发,并掌握COBOL编程语言的核心知识和技能。
接下来,我将详细介绍COB培训教程的内容、特点、适用人群以及学习路径,希望能为初学者提供有用的指导和帮助。
一、教程内容COB培训教程的内容主要包括COBOL的基本语法、程序结构、数据类型、控制流程、函数库等方面的知识。
详细的内容如下:1. COBOL程序基础:了解COBOL的编程规范、文件组织、数据类型、格式化输出、程序控制等基本知识。
2. COBOL程序结构:了解COBOL程序的各个部分的作用、使用方法和语法;包括IDENTIFICATION、ENVIRONMENT、DATA、PROCEDURE等部分。
3. COBOL数据类型:了解COBOL的基本数据类型、变量声明、数据结构等方面的知识,包括整型、浮点型、字符型、日期型等数据类型。
4. COBOL控制流程:了解COBOL程序中的分支、循环、条件判断、跳转、异常处理等控制语句,并掌握它们在程序设计中的应用方法。
5. COBOL函数库:熟悉COBOL中常用的函数库,如字符串函数、日期函数、文件函数等,并了解它们的使用方法和技巧。
二、教程特点1.适合初学者:COB培训教程专为初学者打造,用简单易懂的语言讲解COBOL编程语言,不需要任何编程基础即可学习。
2.系统性强:教程内容涵盖了COBOL的全部核心知识和技能,从基础语法到高级应用均有涉及。
3.实战性强:COB培训教程为学生提供丰富的练习题目和编程实例,让学生在实践中学习与掌握相关技能。
4.周到的服务:提供在线答疑,学习过程中遇到任何问题随时可与专业师资沟通交流。
三、适用人群COB培训教程适用于以下人群:1.对IT行业和程序开发感兴趣的初学者。
2.想要快速掌握COBOL编程语言并具备实际开发能力的人员。
3.沐浴在成千上万行COBOL代码之中但从未学习过它的COBOL程序员。
About the T utorialCOBOL stands for Common Business-Oriented Language. The US Department of Defense, in a conference, formed CODASYL (Conference on Data Systems Language) to develop a language for business data processing needs which is now known as COBOL.COBOL is used for writing application programs and we cannot use it to write system software. The applications like those in defense domain, insurance domain, etc. which require huge data processing make extensive use of COBOL. AudienceThis tutorial is designed for software programmers who would like to learn the basics of COBOL. It provides enough understanding on COBOL programming language from where you can take yourself to a higher level of expertise. PrerequisitesBefore proceeding with this tutorial, you should have a basic understanding of computer programming terminologies and JCL. A basic understanding of any of the programming languages will help you understand the concepts of COBOL programming and move fast on the learning track.Copyright & DisclaimerCopyright 2014 by Tutorials Point (I) Pvt. Ltd.All the content and graphics published in this e-book are the property of Tutorials Point (I) Pvt. Ltd. The user of this e-book is prohibited to reuse, retain, copy, distribute or republish any contents or a part of contents of this e-book in any manner without written consent of the publisher.We strive to update the contents of our website and tutorials as timely and as precisely as possible, however, the contents may contain inaccuracies or errors. Tutorials Point (I) Pvt. Ltd. provides no guarantee regarding the accuracy, timeliness or completeness of our website or its contents including this tutorial. If you discover any errors on our website or inthistutorial,******************************************T able of ContentsAbout the Tutorial (i)Audience (i)Prerequisites (i)Copyright & Disclaimer (i)Table of Contents ...................................................................................................................................... i i 1. OVERVIEW . (1)Introduction to COBOL (1)Evolution of COBOL (1)Importance of COBOL (1)Features of COBOL (2)2. ENVIORNMENT SETUP (3)Installing COBOL on Windows/Linux (3)Compiling COBOL Programs (3)Executing COBOL Programs (4)Executing COBOL-DB2 programs (4)Try it Option Online (6)3. PROGRAM STRUCTURE (7)Divisions (8)4. BASIC SYNTAX (12)Character Set (12)Coding Sheet (13)Character Strings (14)Comment (14)Literal (16)COBOL Word (17)5. DATA TYPES (19)Data Name (19)Level Number (20)Picture Clause (21)Value Clause (22)6. BASIC VERBS (24)Input / Output Verbs (24)Initialize Verb (25)Move Verb (26)Legal Moves (28)Add Verb (28)Subtract Verb (30)Multiply Verb (32)Divide Verb (33)Compute Statement (34)7. DATA LAYOUT (36)Redefines Clause (36)Renames Clause (37)Usage Clause (38)Copybooks (40)8. CONDITIONAL STATEMENTS (41)IF Condition Statement (41)Relation Condition (42)Sign Condition (44)Class Condition (45)Condition-name Condition (46)Combined Condition (48)Evaluate Verb (49)9. LOOP STATEMENTS (51)Perform Thru (51)Perform Until (52)Perform Times (53)Perform Varying (54)GO TO Statement (55)10. STRING HANDLING (57)Inspect (57)String (59)Unstring (60)11. TABLE PROCESSING (63)Table Declaration (63)Subscript (65)Index (66)Set Statement (67)Search (69)Search All (69)12. FILE HANDLING (72)Field (72)Record (73)File (73)13. FILE ORGANIZATION (74)Sequential File Organization (74)Relative File Organization (75)14. FILE ACCESS MODE (77)Sequential Access (77)Random Access (78)Dynamic Access (79)15. FILE HANDLING VERBS (81)Open Verb (81)Read Verb (82)Write Verb (85)Rewrite Verb (87)Delete Verb (89)Start Verb (90)Close Verb (91)16. SUBROUTINES (92)Call Verb (92)Call By Reference (92)Call By Content (94)Types of Call (95)17. INTERNAL SORT (96)Sort Verb (96)Merge Verb (98)18. DATABASE INTERFACE (101)Embedded SQL (101)DB2 Application Programming (101)Host Variables (101)SQLCA (102)SQL Queries (103)Cursors (109)19. INTERVIEW QUESTIONS (112)What is Next? (116)1. OVERVIEWIntroduction to COBOLCOBOL is a high-level language. One must understand the way COBOL works. Computers only understand machine code, a binary stream of 0s and 1s. COBOL code must be converted into machine code using a compiler. Run the program source through a compiler. The compiler first checks for any syntax errors and then converts it into machine language. The compiler creates an output file which is known as load module. This output file contains executable code in the form of 0s and 1s. Evolution of COBOLDuring 1950s, when the businesses were growing in the western part of the world, there was a need to automate various processes for ease of operation and this gave birth to a high-level programming language meant for business data processing.∙In 1959, COBOL was developed by CODASYL (Conference on Data Systems Language).∙The next version, COBOL-61, was released in 1961 with some revisions.∙In 1968, COBOL was approved by ANSI as a standard language for commercial use (COBOL-68).∙It was again revised in 1974 and 1985 to develop subsequent versions named COBOL-74 and COBOL-85 respectively.∙In 2002, Object-Oriented COBOL was released, which could use encapsulated objects as a normal part of COBOL programming.Importance of COBOL∙COBOL was the first widely used high-level programming language. It is an English-like language which is user friendly. All the instructions can be coded in simple English words.∙COBOL is also used as a self-documenting language.∙COBOL can handle huge data processing.7∙COBOL is compatible with its previous versions.∙COBOL has effective error messages and so, resolution of bugs is easier. Features of COBOLStandard LanguageCOBOL is a standard language that can be compiled and executed on machines such as IBM AS/400, personal computers, etc.Business OrientedCOBOL was designed for business-oriented applications related to financial domain, defense domain, etc. It can handle huge volumes of data because of its advanced file handling capabilities.Robust LanguageCOBOL is a robust language as its numerous debugging and testing tools are available for almost all computer platforms.Structured LanguageLogical control structures are available in COBOL which makes it easier to read and modify. COBOL has different divisions, so it is easy to debug.82. ENVIORNMENT SETUPInstalling COBOL on Windows/LinuxThere are many Free Mainframe Emulators available for Windows which can be used to write and learn simple COBOL programs.One such emulator is Hercules, which can be easily installed on Windows by following a few simple steps as given below:∙Download and install the Hercules emulator, which is available from the Hercules' home site :www.hercules-390.eu∙Once you have installed the package on Windows machine, it will create a folder like C:/hercules/mvs/cobol.∙Run the Command Prompt (CMD) and reach the directory C:/hercules/mvs/cobol on CMD.∙The complete guide on various commands to write and execute a JCL and COBOL programs can be found at:/hercules/installmvs/instmvs2.htmHercules is an open-source software implementation of the mainframe System/370 and ESA/390 architectures, in addition to the latest 64-bit z/Architecture. Hercules runs under Linux, Windows, Solaris, FreeBSD, and Mac OS X.A user can connect to a mainframe server in a number of ways such as thin client, dummy terminal, Virtual Client System (VCS), or Virtual Desktop System (VDS). Every valid user is given a login id to enter into the Z/OS interface (TSO/E or ISPF). Compiling COBOL ProgramsIn order to execute a COBOL program in batch mode using JCL, the program needs to be compiled, and a load module is created with all the sub-programs. The JCL uses the load module and not the actual program at the time of execution. The load libraries are concatenated and given to the JCL at the time of execution using JCLLIB or STEPLIB.9There are many mainframe compiler utilities available to compile a COBOL program. Some corporate companies use Change Management tools like Endevor, which compiles and stores every version of the program. This is useful in tracking the changes made to the program.//COMPILE JOB,CLASS=6,MSGCLASS=X,NOTIFY=&SYSUID//*//STEP1 EXEC IGYCRCTL,PARM=RMODE,DYNAM,SSRANGE//SYSIN DD DSN=MYDATA.URMI.SOURCES(MYCOBB),DISP=SHR//SYSLIB DD DSN=MYDATA.URMI.COPYBOOK(MYCOPY),DISP=SHR//SYSLMOD DD DSN=MYDATA.URMI.LOAD(MYCOBB),DISP=SHR//SYSPRINT DD SYSOUT=*//*IGYCRCTL is an IBM COBOL compiler utility. The compiler options are passed using the PARM parameter. In the above example, RMODE instructs the compiler to use relative addressing mode in the program. The COBOL program is passed using the SYSIN parameter. Copybook is the library used by the program in SYSLIB. Executing COBOL ProgramsGiven below is a JCL example where the program MYPROG is executed using the input file MYDATA.URMI.INPUT and produces two output files written to the spool. //COBBSTEP JOB CLASS=6,NOTIFY=&SYSUID////STEP10 EXEC PGM=MYPROG,PARM=ACCT5000//STEPLIB DD DSN=MYDATA.URMI.LOADLIB,DISP=SHR//INPUT1 DD DSN=MYDATA.URMI.INPUT,DISP=SHR//OUT1 DD SYSOUT=*//OUT2 DD SYSOUT=*//SYSIN DD *//CUST1 1000//CUST2 1001/*10The load module of MYPROG is located in MYDATA.URMI.LOADLIB. This is important to note that the above JCL can be used for a non-DB2 COBOL module only. Executing COBOL-DB2 programsFor running a COBOL-DB2 program, a specialized IBM utility is used in the JCL and the program; DB2 region and required parameters are passed as input to the utility. The steps followed in running a COBOL-DB2 program are as follows:∙When a COBOL-DB2 program is compiled, a DBRM (Database Request Module) is created along with the load module. The DBRM contains the SQL statements of the COBOL programs with its syntax checked to be correct.∙The DBRM is bound to the DB2 region (environment) in which the COBOL will run. This can be done using the IKJEFT01 utility in a JCL.∙After the bind step, the COBOL-DB2 program is run using IKJEFT01 (again) with the load library and the DBRM library as the input to the JCL.//STEP001 EXEC PGM=IKJEFT01//*//STEPLIB DD DSN=MYDATA.URMI.DBRMLIB,DISP=SHR//*//input files//output files//SYSPRINT DD SYSOUT=*//SYSABOUT DD SYSOUT=*//SYSDBOUT DD SYSOUT=*//SYSUDUMP DD SYSOUT=*//DISPLAY DD SYSOUT=*//SYSOUT DD SYSOUT=*//SYSTSPRT DD SYSOUT=*//SYSTSIN DD *DSN SYSTEM(SSID)RUN PROGRAM(MYCOBB) PLAN(PLANNAME) PARM(parameters to cobol program) - LIB('MYDATA.URMI.LOADLIB')11END/*In the above example, MYCOBB is the COBOL-DB2 program run using IKJEFT01. Please note that the program name, DB2 Sub-System Id (SSID), and DB2 Plan name are passed within the SYSTSIN DD statement. The DBRM library is specified in the STEPLIB.Try it Option OnlineYou really do not need to set up your own environment to start learning COBOL programming language. Reason is very simple, we have already set up COBOL Programming environment online, so that you can compile and execute all the available examples online at the same time, when you are doing your theory work. This gives you confidence in what you are reading and to check the result with different options. Feel free to modify any example and execute it online.Try the following example using our Try it option available alongside the code in our website.IDENTIFICATION DIVISION.PROGRAM-ID. HELLO.PROCEDURE DIVISION.DISPLAY 'Hello World'.STOP RUN.When you compile and execute the above program, it produces the following result: Hello WorldFor some of the examples given in this tutorial, you will find a Try it option in our website code sections at the top right corner that will take you to the online compiler. So just make use of it and enjoy your learning. Try it option would work only with the code compatible with OpenCOBOL. The programs that require JCL (Input file, Output file or Parameters) for execution would not run on Try it option.123. PROGRAM STRUCTUREA COBOL program structure consists of divisions as shown in the following image:A brief introduction of these divisions is given below:∙Sections are the logical subdivision of program logic. A section is a collection of paragraphs.∙Paragraphs are the subdivision of a section or division. It is either a user-defined or a predefined name followed by a period, and consists of zero or more sentences/entries.∙Sentences are the combination of one or more statements. Sentences appear only in the Procedure division. A sentence must end with a period.∙Statements are meaningful COBOL statements that perform some processing.∙Characters are the lowest in the hierarchy and cannot be divisible.You can co-relate the above-mentioned terms with the COBOL program in the following example:PROCEDURE DIVISION.A0000-FIRST-PARA SECTION.FIRST-PARAGRAPH.13ACCEPT WS-ID - Statement-1 -----|MOVE '10' TO WS-ID - Statement-2 |-- Sentence - 1DISPLAY WS-ID - Statement-3 -----|.DivisionsA COBOL program consists of four divisions.Identification DivisionIt is the first and only mandatory division of every COBOL program. The programmer and the compiler use this division to identify the program. In this division, PROGRAM-ID is the only mandatory paragraph. PROGRAM-ID specifies the program name that can consist 1 to 30 characters.Try the following example using the Try it option online.IDENTIFICATION DIVISION.PROGRAM-ID. HELLO.PROCEDURE DIVISION.DISPLAY 'Welcome to Tutorialspoint'.STOP RUN.Given below is the JCL to execute the above COBOL program.//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C//STEP1 EXEC PGM=HELLOWhen you compile and execute the above program, it produces the following result: Welcome to TutorialspointEnvironment DivisionEnvironment division is used to specify input and output files to the program. It consists of two sections:Configuration section provides information about the system on which the program is written and executed. It consists of two paragraphs:14o Source computer : System used to compile the program.o Object computer : System used to execute the program.∙Input-Output section provides information about the files to be used in the program. It consists of two paragraphs:o File control : Provides information of external data sets used in the program.o I-O control : Provides information of files used in the program.ENVIRONMENT DIVISION.CONFIGURATION SECTION.SOURCE-COMPUTER. XXX-ZOS.OBJECT-COMPUTER. XXX-ZOS.INPUT-OUTPUT SECTION.FILE-CONTROL.SELECT FILEN ASSIGN TO DDNAMEORGANIZATION IS SEQUENTIAL.Data DivisionData division is used to define the variables used in the program. It consists of four sections:∙File section is used to define the record structure of the file.∙Working-Storage section is used to declare temporary variables and file structures which are used in the program.∙Local-Storage section is similar to Working-Storage section. The only difference is that the variables will be allocated and initialized every time a program starts execution.∙Linkage section is used to describe the data names that are received from an external program.COBOL ProgramIDENTIFICATION DIVISION.PROGRAM-ID. HELLO.15ENVIRONMENT DIVISION.INPUT-OUTPUT SECTION.FILE-CONTROL.SELECT FILEN ASSIGN TO INPUT.ORGANIZATION IS SEQUENTIAL.ACCESS IS SEQUENTIAL.DATA DIVISION.FILE SECTION.FD FILEN01 NAME PIC A(25).WORKING-STORAGE SECTION.01 WS-STUDENT PIC A(30).01 WS-ID PIC 9(5).LOCAL-STORAGE SECTION.01 LS-CLASS PIC 9(3).LINKAGE SECTION.01 LS-ID PIC 9(5).PROCEDURE DIVISION.DISPLAY 'Executing COBOL program using JCL'.STOP RUN.The JCL to execute the above COBOL program is as follows://SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C//STEP1 EXEC PGM=HELLO//INPUT DD DSN=ABC.EFG.XYZ,DISP=SHRWhen you compile and execute the above program, it produces the following result: Executing COBOL program using JCLProcedure Division16Procedure division is used to include the logic of the program. It consists of executable statements using variables defined in the data division. In this division, paragraph and section names are user-defined.There must be at least one statement in the procedure division. The last statement to end the execution in this division is either STOP RUN which is used in the calling programs or EXIT PROGRAM which is used in the called programs.IDENTIFICATION DIVISION.PROGRAM-ID. HELLO.DATA DIVISION.WORKING-STORAGE SECTION.01 WS-NAME PIC A(30).01 WS-ID PIC 9(5) VALUE '12345'.PROCEDURE DIVISION.A000-FIRST-PARA.DISPLAY 'Hello World'.MOVE 'TutorialsPoint' TO WS-NAME.DISPLAY "My name is : "WS-NAME.DISPLAY "My ID is : "WS-ID.STOP RUN.JCL to execute the above COBOL program://SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C//STEP1 EXEC PGM=HELLOWhen you compile and execute the above program, it produces the following result:Hello WorldMy name is : TutorialsPointMy ID is : 12345174. BASIC SYNTAXCharacter Set'Characters' are lowest in the hierarchy and they cannot be divided further. The COBOL Character Set includes 78 characters which are shown below: Character DescriptionA-Z Alphabets(Upper Case)a-z Alphabets (Lower Case)0-9 NumericSpace+ Plus Sign- Minus Sign or Hyphen* Asterisk/ Forward Slash$ Currency Sign, Comma; Semicolon18. Decimal Point or Period" Quotation Marks( Left Parenthesis) Right Parenthesis> Greater than< Less than: Colon' Apostrophe= Equal SignCoding SheetThe source program of COBOL must be written in a format acceptable to the compilers. COBOL programs are written on COBOL coding sheets. There are 80 character positions on each line of a coding sheet.Character positions are grouped into the following five fields:Positions Field Description1-6 Column Numbers Reserved for line numbers.7 Indicator It can have Asterisk (*) indicating comments,Hyphen (-) indicating continuation and Slash (/ ) indicating form feed.198-11 Area A All COBOL divisions, sections, paragraphs andsome special entries must begin in Area A.12-72 Area B All COBOL statements must begin in area B.73-80 Identification Area It can be used as needed by the programmer.ExampleThe following example shows a COBOL coding sheet:000100 IDENTIFICATION DIVISION. 000100000200 PROGRAM-ID. HELLO. 000101000250* THIS IS A COMMENT LINE 000102000300 PROCEDURE DIVISION. 000103000350 A000-FIRST-PARA. 000104000400 DISPLAY “Coding Sheet”. 000105 000500 STOP RUN. 000106JCL to execute the above COBOL program://SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C//STEP1 EXEC PGM=HELLOWhen you compile and execute the above program, it produces the following result:Coding Sheet20Character StringsCharacter strings are formed by combining individual characters. A character string can be a∙Comment,∙Literal, or∙COBOL word.All character strings must be ended with separators. A separator is used to separate character strings.Frequently used separators : Space, Comma, Period, Apostrophe, Left/Right Parenthesis, and Quotation mark.CommentA comment is a character string that does not affect the execution of a program. It can be any combination of characters.There are two types of comments:Comment LineA comment line can be written in any column. The compiler does not check a comment line for syntax and treats it for documentation.Comment EntryComment entries are those that are included in the optional paragraphs of an Identification Division. They are written in Area B and programmers use it for reference.The text highlighted in Bold are the commented entries in the following example: 000100 IDENTIFICATION DIVISION. 000100000150 PROGRAM-ID. HELLO. 000101000200 AUTHOR. TUTORIALSPOINT. 000102000250* THIS IS A COMMENT LINE 00010321000300 PROCEDURE DIVISION. 000104000350 A000-FIRST-PARA. 000105000360/ First Para Begins - Documentation Purpose 000106000400 DISPLAY “Comment line”. 000107 000500 STOP RUN. 000108JCL to execute above COBOL program://SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C//STEP1 EXEC PGM=HELLOWhen you compile and execute the above program, it produces the following result: Comment Line22End of ebook previewIf you liked what you saw…Buy it from our store @ https://23。
cobol 语言.修订版 .(上册+下册).pdf 2012-12-14 (1~22 ) COBOL 语言特点 : (1) 适合数据处理 ( 算术计算量小而逻辑处理多;输入输出数据量大;数据间有一定逻辑关系, 即层次关系;大量分类排序;对报表要求高、多样化等 ) (2) 接近自然语言英语,易于理解 (3) 通用性强,易于移植 (4) 结构严谨,层次分明,每个程序分为 4 部分(division :标示部 Identification division 、 环境部 envroNment division 、数据部 data division 、过程部 procedure division) 、每部 分分为若干节 (section 有节头标示, 类似部头 ) 、节下面分为若干段 (paragraph 有段头标示 ) 。 每一部分都有固定格式。 (5) 缺点是比较繁琐。不乱程序逻辑如何,都必须按照四大部分生成,对每部分都必须定义和 说明,即使该部分实际上没有内容,也要写出头部,导致源程序冗长。 ================= 一个简单的 COBOL 程序: Code: IDENTIFICATION DIVISION. PROGRAM-ID. EXAM1. ENVIRONMENT DIVISION. DATA DIVISION. PROCEDURE DIVISION. S. DISPLAY 'THIS IS A COBOL PROGRAM.' STOP RUN. ================= IDENTIFICATION DIVISION: 用来指定源程序名称,也可写入其他信息 ( 如作者、日期、保 密等) 。 ENVIRONMENT DIVISION: 程序中用到的数据文件名和计算机的设备的对应关系, 即把某一 数据文件和一外部设备关联起来。 此外,还可以指定目标程序中使用的专门控制方法和程序所用 内存区的大小。DATA DIVISION: 程序用到的所有数据的类型和占用内存的情况。 PROCEDURE DIVISION: 用来给出程序要执行的命令,是计算机产生相应的操作。 部下面可以有节,节下面有段;或者部下面直接是段。
================= 以上四个部分,只有过程部是执行部分,是核心部分。 过程部中,每段由若干句 (Sentence) 组成,每句以句号加一个以上的空格来结束的。 一个句子只能由一个语句组成,一个语句加一个句号后跟一个或多个空格形成一个句子。 一个句子可以包含多个子句 (Clause) ,每个子句也有一个动词,指定某一方面的功能。 除了过程部外,环境部和数据部也可以包含子句,如 SELECT 子句、文件描述子句等。
================= COBOL 每行 80 列,每行分为几个区: (1) 1~6 列为 “标号区 ”,一般前 3 列表示页号,后 3 列表示本页行号。标号应当按照从小到大 顺序填写,但可以不连续。可以填标号,也可以不填写,对程序没有影响。 (2) 第 7 列: “续行标志区 ”,写上续行符 "-" ,表示本行是续上一行后面的。一般只有拆分字符 需要两行紧连或者一个长的字符串需要拆成两行时才使用,否则可以空着。不推荐使用续行符, 容易导致错误。 (3) 第 8~11 列,称为 "A 区" ,程序的某些部分,如部头、节头、段头、层号 01 、层号 77 以 及文件标示符 FD 等应从 A 区开始书写。可以从第 8 列开始,也可以不从,但必须在 8~11 列 的任何一列开始。 (4) 第 12~72 列,称为 "B 区",是程序的正文部分,过程部的句子必须从 B 区开始写,不能写 入 A 区中。 (5) 第 73~80 列,为 “注释区 ”,一般是简单说明,程序编译和执行时都不会理会该区内容。 80 列的划分区,只是对程序有以上限制,对于数据而言没有以上分区限制,即输入数据可以从 第 1 列用到第 80 列。
所有字母应大写; 相邻两个字之间应该留一个或以上空格; 运算符和等号两边必须各留一个空格; 过程部中左括号的左侧和右括号的右侧都要留一个空格, 内侧不必留空格;逗号、句号、分号的 左边不能留空格,右边必须留空格;一个空格和多个空格作用相同,但引号中的除外; 允许一行写几个语句,也可以一个语句写在多行上。 ================= COBOL 字符集 :COBOL 程序中允许出现的字符 ( 引号内的除外 ) 。 数字: 0~9 大写字母: A~Z 专用字符 15 个: 加号 + 减号或连接号 - 乘号或星号 * 除号 / 等号 = 逗号 , 句号或小数点 . 分号 ; 引号 '或" 括号 () 小于号 < 大于号 > 空格 货币符号 $
#,!,?,% 等字符属于系统字符集但不属于 COBOL 字符集,不能在程序中出现 ( 引号内除外 ) 。 ================= COBOL 字 分为两类:保留字和用户字 ( 非保留 )。 保留字 指在 COBOL 中已规定作专门用途的字,代表特定的含义,不能另作他用。
================= 数据名 就是由程序设计者定义的一片内存区域,即用一个符号代表一个地址。长度只能是 1~30 个字符之间;只能由字母、数字和连接符 "-" 组成,至少含一个字母,连接符 不能出现在名称的两端, 不能出现空格; 不能使用 COBOL 保留字, 一般使用连接符是避免使用 保留字的好方法,或者使用 X 或数字开头的名称;最好使用望文生义的单词命名。
================= 常量 包括数值常量 (Numerical Literal) 、非数值常量 (Nonnumerical Literal) 和表意常量 (Figurative Constant) 三类。
数值常量 不能超过 18 位数字;至少要有一个数字,不能含有超过一个符号;数字之间不能有空 格。
非数值常量 指用引号引起来的字符串; 非数值含量不能参与运算; 如果想把引号也放入常量, 需 要使用 QUOTE 保留字实现,如 DISPLAY QUOTE 'ABCD' QUOTE ,表示 'ABCD' 六个字符, 但 QUOTE 本身不能替代引号;非数值常量不能超过 120 个字符。
表意常量 就是指用保留字表示的一些特殊常量,如用 ZERO 表示 0 ,SPACE 表示空格等。 ================= COBOL 的数据具有层次的特征,类似于一颗倒立的树或者是金字塔结构,一般将这样的一条数 据称之为一个记录 (record) 。为了反映数据在一个层次结构中的层次,引入了层号的概念,使 用 2 位数来表示。层号小表示较高的层次。 记录 是具有一定层次关系的一组数据项的最大集合。是内存中具有独立逻辑含义的最大可存取 项,层号为 01 。 记录下分为几项,不能继续分的称为 初等项或基本项 (Element item) 。初等项是数据的基本单 位;如果还能继续分,称为 组合项 (Group Item) ,组合项下可以是组合项或者初等项。 多个记录可以组成一个 文件 。但文件一般存储在外部,不在内存中,内存中最大存取项是记录。2012-12-19 23~28 过程部是 COBOL 的核心部分,应当以过程部为主体,找出几部分之间的关系。 过程部的特点 : (1) 过程部是程序的第四部分 (2) 过程部都以一个动词开始,如 MOVE 、READ 、WRITE 、IF 。表示计算机应当执行的操作。 (3) 动词后面一般都跟着一个操作的对象,可以是数据文件或者数据名。 (4) 过程部一律写在 B 区上,即第 12 列开始,可以写在一行或者多行,续行也从 B 区开始。 ========= 输入输出 1、ACCEPT 语句 从键盘终端或者指定输入设备获取少量输入数据。 ACCEPT 标识符 [FROM 助忆名 ] 这里的标识符必须是可以唯一的标识一个数据项的数据名。 如部-> 节-> 段-> 数据项的情况下,使用 OF 关联上下关系,保证标识符的唯一性,如 01 A. 02 B1. 03 C ... 03 D ... 02 B2. 03 C ... 03 D ... 必须写成 C OF B1 和 C OF B2 区分两个不同的 C
以上[] 内的内容表示是可选项, {} 内的内容表示其中各项可多选一, ... 表示之前的 {} 或[] 的内 容可以重复多次,有下划线的保留字表示必写,没有的表示可以省略。
以上的 FROM 省略时表示从系统默认输入设备获取数据,如键盘,增加时,表示从指定设备获 取数据,助忆名 表示从一个设备名,具体的名称可以在 ENVIRONMENT DIVISION 中定义: ENVIRONMENT DIVISION.CONFIGURATION SECTION. 配置节 SPECIAL-NAMES. 专用字段 CONSOLE IS ABC. 将输入设备定义为助忆名: ABC ACCEPT T FROM ABC. 表示从输入设备获取数据并赋值给变量 T。 ACCEPT 之后只能跟一个标识符,不能跟多个。 ========= 2、DISPLAY 语句 用于少量数据输出到指定外设上。 DISPLAY { 标识符 | 常量}[ 标识符 | 常量]...[UPON 助忆名 ] 默认在屏幕上显示数据,使用 UPON 指向指定的设备名输出 每执行一次 DISPLAY ,总是从一个新行开始;如果一行显示不完,会自动换行显示。
正式程序中,一般都不使用 ACCEPT 和 DISPLAY ,以提高系统效率,减少手工干预。 ========= 3、READ 语句 READ 文件名 RECORD [INTO 标识符 ] [;AT END 执行语句 ] 分号可以省略 例如 READ IN-FILE ,表示从 IN-FILE 文件中读取数据, IN-FILE 称为 “内部文件名 ”,它与外 部文件名之间的关系是通过 ENVIRONMENT DIVISION 中指定的,如: Code: ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. 输入输出节 FILE-CONTROL. 文件控制段 SELECT IN-FILE ASSIGN TO 外部文件名 .