21
INTRODUCTION TO COBOL PROGRAMMING Chapter 1 CISB323 Business Programming

Chapter 1

Embed Size (px)

DESCRIPTION

chapter 1

Citation preview

INTRODUCTION TO COBOL PROGRAMMINGChapter 1 CISB323 Business ProgrammingStructure of COBOL ProgramsPROGRAMDIVISION(s)SECTION(s)Paragraph(s)Sentence(s)Statement(s)A COBOL Program000100 IDENTIFICATION DIVISION000!00 PROGRAM"ID #e$$%000&00 ENVIRONMENT DIVISION000'00 DATA DIVISION000(00 PROCED)RE DIVISION000*00000+00 PROGRAM",EGIN000-00DISP.A/ 0#e$$% 1%r$20000300001000 PROGRAM"DONE001100STOP R)NDivisionsParagraphSentenceStatementCOBOL Program Parts : DivisionsThere are four divisions in COBOL.ID!TI"IC#TIO! DI$ISIO!!$I%O!&!T DI$ISIO!D#T# DI$ISIO! P%OCD'% DI$ISIO!DO!(Tma)et*pingerrors.Someofthet*pingerrorsthatcancause serious pro+,ems -hen *ou are compi,ing inc,ude misspe,,ing the name of a DI$ISIO! .for e/amp,e0 I!D!TI"IC#TIO! DI$ISIO!10 adding an unnecessar* h*phen .for e/amp,e0 D#T#2DI$ISIO!10 or omitting an* of the periods. !ote that ever*thing ends -ith a period ..1COBOL rogram arts : DivisionsID!TI"IC#TIO! DI$ISIO! mar)s the +eginning of a COBOL program. The name of the program0 -hich *ou assign0 -i,, +e entered as a statement in this divisionThe program name shou,d +e uni3ue for each program and shou,d +e ,in)ed -ith the name of source fi,eThis division is used to identif* +asic information a+out the program. In previous e/amp,e0 the ID!TI"IC#TIO! DI$ISIO! contains on,* the P%O4%#&2ID0 5e,,o.COBOL rogram arts : DivisionsID!TI"IC#TIO! DI$ISIO! must contain at ,east the program name. Other usefu, information that *ou can inc,ude are6#'T5O% 2 so -e -i,, )no- -ho to +,ame -hen things go -rong7D#T28%ITT! 2 this gives us the age of the origina, version.D#T2CO&PILD 2 this is automatica,,* inserted +* the COBOL compi,er0 and sho-s the age of the current version of this program.COBOL rogram arts : Divisions!$I%O!&!TDI$ISIO!0-hichisusedtoidentif* theenvironmentin-hichtheprogramisrunning. %emem+erthatCOBOLisintendedtorunonman* different t*pes of machines0 and this section is used to hand,e the differences +et-een various computers.Containsstatementsorcommandstodescri+ethe ph*sica, environment in -hich the program is running. Themainuseofthe!$I%O!&!TDI$ISIO!isto descri+e the ph*sica, structure of fi,es that -i,, +e used in the program.COBOL rogram arts : DivisionsD#T#DI$ISIO!containsstatementsdescri+ingthe data used +* the program.Contain an* data that the program operates on.#,,thedatathattheprogramuses-i,,+edefinedin this division.D#T# DI$ISIO! and P%OCD'% DI$ISIO! are the mostimportantdivisionsinaCOBOLprogram9the* do :; percent of the -or).COBOL rogram arts : DivisionsP%OCD'%DI$ISIO!.Thisisthemeatofthe program22thepartthatdoesthe-or)intended+*the programmer.Contains the COBOL statements that the program -i,, e/ecute after the program starts running. The rea, -or)horse of a COBOL program. 8ithout a P%OCD'% DI$ISIO!0 *ou -ou,dn(t have a program0 +ecause a,, the other divisions are used to create the environment and data that are used +* the P%OCD'% DI$ISIO! to actua,,* do something.COBOL rogram arts : Program!ID an" Sto Run&ost compi,ers re3uire that on,* t-o things +e present in aCOBOLprogram22otherthanthefourdivisions22in order to compi,e it6P%O4%#&2IDSTOP %'!P%O4%#&2IDisaparagraphthatmustappearinthe ID!TI"IC#TIO!DI$ISIO!andisusedtogivethe program a name.There must a,so +e one paragraph in the P%OCD'% DI$ISIO! that contains the STOP %'! statement.&ostversionsofCOBOLre3uirethise/p,icitcommand asa-a*ofidentif*ingthepointintheprogram-here the program terminates.COBOL # Sam$e rogram000100 IDENTIFICATION DIVISION000!00 PROGRAM"ID Sentences000&00 ENVIRONMENT DIVISION000'00 DATA DIVISION000(00 PROCED)RE DIVISION000*00000+00 PROGRAM",EGIN000-00DISP.A/ 0Th4s pr%gram c%nta4ns 5%6r DIVISIONS70000300DISP.A/ 0three PARAGRAP#S0001000DISP.A/ 0an2 5%6r SENTENCES0001100 PROGRAM"DONE001!00STOP R)NCOBOL Source Co"e#COBOLsourcecodefi,ehasfiveareas0e/tending from ,eft to right across the page.COBOL Source Co"eThe first si/ characters or co,umns of a ,ine are ca,,ed the se3uence num+er area.Thisareaisnotprocessed+*thecompi,er0orifitis processed0itprovides*ouon,*-ith-arningsthatnum+ers are out of se3uence .if the* are1.Character position < is ca,,ed the indicator area.This seventh position is usua,,* +,an).If an asteris) .=1 is p,aced in this co,umn0 ever*thing e,se on that ,ine is ignored +* the compi,er. Thisisusedasamethodtoinc,udecommentsin*our source code fi,e.COBOL source co"e : Area A vs Area BThefourcharacterpositions>through11areca,,ed #rea #.DI$ISIO!sandparagraphs.andSCTIO!s1muststartin #rea #. ItisgoodcodingpracticetostartDI$ISIO!s0SCTIO!s0 and paragraph names at co,umn > rather than some random p,ace in #rea #.Character positions 12 through