home *** CD-ROM | disk | FTP | other *** search
- 0660103030566
- 1
- 2page #
- 9[....................................................]
-
-
- ëFirstlessonin
- Ç
-
- ü******PASCAL******
-
-
- bySERGEVAILLANCOURT
-
-
- ÇWHYPASCAL?
-
- Untiltheendofthe60's,animportantaspectofprogram
- writingwasthattheyhadtobewrittenintheshortestpossible
- timeinordertosavethecompaniessomemoney.Thismentalitywas
- foundtobesomewhatflawedforthesimplereasonthatthe
- programmers,havinglesstimetowritetheprograms,oftentraded
- readabilityforspeed.Themaintenanceandupdatingofthese
- programswasthereforelenghtier,moreexpensiveand,sincesucha
- programhasanaverageusefullifeofabout10years,...
-
- Neartheendofthatdecade,anindividualnamedüNIKLAUS
- WIRTHÇcreatedastructuredlanguagethatüÇfinallyansweredthe
- programmer'sneedsforreadableandmaintainablecode.Hecalled
- thatlanguage"üPASCALÇ".
-
- ëStructureofaPASCALprogramÇ
-
- äPROGRAM
- Ç|
- Programname
- |
- Program'sparameterlist
- |
- declarations
- |
- äBEGINüÇ⇦---+
- ||
- instructionsüÇ⇧
- üÇ||
- +---⇦---+---⇨---+
- |
- äEND.
- Ç|
- (figure1.)
-
-
- Forclarity,theconventionfromnowonwillbethatall
- wordsinäITALICSÇarePASCALreservedwordsandthattheyhavea
- specialsignificanceinthis environment.Youmustthereforeuse
- themonlywheretheybelongandthesamegoesforthe"ä;Ç"
- (semicolon),the"."(period),andthe"ä,Ç"(comma).
-
- Figure1explainsthestructureofaüPASCALÇprogram.
-
- àPROGRAMÇ:Alwaysstartaprogramwiththisreservedword.
-
- Programname:Anidentifierofyourchoice.Notethatthe
- identifiermustbeanalphanumericstring
- startingwithaletter.
-
- Programparameterlist:IdentifiersusedforInput/Output.
- Fornow,theonlytwoparameterswe
- willuseareàINPUTÇandàOUTPUTÇ,for
- thekeyboardandthescreen,
- respectively.
-
- "à;äÇ"(Semicolon):Indicatestothecompilerthatthe
- programheaderendshere.
-
- Declarations:Identifiesconstants,datatypes,variables,
- procedures,andfunctions.
-
- àBEGINÇ:Theprogramstartswiththisreservedword.
-
- Instructions:Tellstheprogramwhattodonext.
-
- "ü:Ç"(Colon):Usedbetweenprograminstructions.
-
- "àENDÇ"and"ü.Ç"(Period):Usedtospecifytheendofthe
- compilation.
-
- Hereisaprogramthatwillwrite"Hello, my friend"tothe
- screen.
-
- àPROGRAMÇdisplay(àOUTPUTÇ)à
-
- BEGIN
- Çäwriteln;
- writelnÇ('Hello,myfriend');
- àEND.
-
- Ç(üProgram1Ç)
-
- Onthefirstline,wefindthereservedwordàPROGRAM,Çthe
- nameoftheprogram,aswellastheprogram'sparameterlist.
- Sincethisprogramonlywritestothescreen,theonlyparameter
- wewillneedisàOUTPUTä.
- Ç Wehavenodeclarationsandthus,westarttheprogramonthe
- secondlinewithàBEGINÇ.Onthethirdline,wefindanewreserved
- word,äwritelnÇwhichallowsustowriteonscreenandletsus
- proceedtothenextline.
-
- Thesetwolinesareinstructionsandareseparatedbya"à;Ç"
- (Semicolon).
-
- Andfinally,thelastlineendstheprogramwith"àEND.Ç"
-
- ìWRITEêandìWRITELNäÇ
-
- Thesetwoinstructionsallowwritingtothescreen.The
- differencebetweenäWRITEÇandäWRITELNÇisthatthelattercausesa
- carriagereturn,i.e.:thecursorisrepositionnedatthestartof
- thenextline,justlikewithüPRINTÇinBASIC.äWRITEÇdoesnotmove
- thecursorafteritsexecution,justlikeüPRINT;ÇinBASIC.
-
- ThesyntaxforäWRITEÇandäWRITELNÇisasfollows:
-
- àWRITEÇoràWRITELNÇ
- |
- (
- |⇦--------+
- parameter(s),
- |⇨--------+
- )
-
- (üFigure2Ç)
-
- Theseparametersmaybevariables,constants,characters,or
- stringsofcharacters.Wewillexaminelaterthesubjectof
- variablesandconstants,butfornow,let'sexamineanexample
- withcharactersandstrings.
-
- àPROGRAMüÇdisplay(àOUTPUTÇ);
- àBEGIN
- WRITEüÇ('H','e','l','l','o');
- (*writesthecharactersH,e,l,l,o,formingthe
- wordHello*)
- àWRITELNÇ('Hello')
- (*writesthestring"Hello"*)
- àEND.Ç
-
- (üProgram3Ç)
-
- Youhaveprobablynoticedthetextbetween(*and*).These
- arecommentsinPASCAL,identicaltoREMsinBASIC.Fromnowon,
- allprogramsherewillbecommentedinthisfashion.
-
- Itispossible,witheachparameterofàWRITEüÇandàWRITELNÇto
- specifytheamountofrightjustificationoftheoutputbyadding
- ":x",wherexstandsfortheamountofjustificationneeded.Ex.:
-
- àWRITEÇ('Hello:15')
-
- Atthetimeofexecution,theoutputwillbe:
-
- ^^^^^^^^^^^Hello
-
- Forclarity,eachcaret(^)representsasinglespace.
-
- Thevalueafterthe":"maybeaconstant,asintheabove
- example,oravariable.Ifthisvalueisinferiortothenumberof
- characterstobeprinted,thentheinstructionwilloverflowthe
- valuegivenandignorethejustification.
-
- ëDECLARATIONS
- Ç
- Thereare5typesofdeclarations:
-
- Labels;
- Constants;
- Datatypes;
- Variables;
- ProceduresandFunctions.
-
- ëLABELSÇ
-
- Theyare"pointers"muchlikeGOTOsinBASIC.Theyareseldom
- usedsincetheycaneasilybereplacedwithotherinstructionsand
- besides,they"break"thestructureoftheprogram.Somepeople
- usethemasa"breakpoint",awaytogetoutofaprogramwhen
- there'sanerrorintheexecutionofthatprogram.Thattopicwill
- beexaminedinalatertutorialonthesubject.
-
- ëCONSTANTSÇ
-
- Aconstantisrepresentedbyanidentifier(theconstant's
- name)thevalueofwhichcannotbechangedduringtheprogram's
- execution.Thisisoftenusedbyprogrammersbecauseitaffords
- themimpeccableclarityandeasestheprogram'smaintenance.
-
- Syntax:
-
- CONSTANT
- |⇦--------+
- identifier|
- |⇧
- =;
- ||
- value|
- |⇨--------+
- ;
-
- (üFigure3Ç)
-
- Thevaluemaybeaninteger,arealnumber,acharacter,ora
- stringofcharacters,thevalueofwhichmustbebetweensingle
- quotes(').
-
- Examples:header='AccountingSystem';
- pi=3.1416;
- CarriageReturn=13;
- MyName='SergeVaillancourt';
-
- Somecompilershavepredefinedconstantsthatdonothaveto
- bedeclaredlike,forexample:äMAXINTÇwhichrepresentsthe
- largestusableinteger,i.e.:32767
-
- ëVARIABLESÇ
-
- Avariableisanidentifier(thevariable'sname),the
- valueofwhichmaybechangedduringtheprogram'sexecution.
- Thus,avariableiscomposedoftwoparts:the"container"which
- isthenameofthevariable,andthe"contents"whichisthevalue
- ofthevariable.
-
- Awayofgivingavariableavalueisby"Assignment".
-
- Variable
- |
- ü:=Ç
- |
- Expression
-
- (üFigure4Ç)
-
- Examples: a:=10
- x:=10+2
- name:='Serge'
-
- The":="symbolisformedbyusingthecharacters":"and"="
- withoutaninterveningspacebetweenthetwo.
-
- InPASCAL,everyvariablemustbedeclared.
-
- Variable
- +----⇨-------|-------⇦----+
- |Variable'sname|
- ;|-------⇨----+
- |Variable'stype
- +----⇦-------|
- ;
-
- (üFigure5Ç)
-
- Thevariable'stypeidentifiesthekindofvariableyouareusing:
- aninteger,arealnumber,acharacter,etc....
-
- ëTHETYPESÇ
-
- Whenwedeclareavariable,wemustgiveitaTYPEattribute.
-
- Example:
-
- àPROGRAMÇexample(àOUTPUTÇ);
- àVARäÇ
- i,number1:äintegerÇ(*typeinteger*)
- number2:ärealÇ(*typereal*)
- character:ächarÇ(*typecharacter*)
- àBEGINÇ
- i:=12;(*assignstoianinitial
- valueof12*)
- number1:=i+3;(*assignstonumber1an
- initialvalueofi+3*)
- àwritelnüÇ('I=',i,'andNumber1='number1);
- number2:=3.8;(*assignstonumber2an
- initialvalueof3.8*)
- character:='A';(*assignstocharacter
- thecharacter'A'*)
- àwritelnÇ('Number2=',number2);
- àwritelnÇ('Character=',character);
- i:=number1+i;(*itakesavalueequal
- tonumber1+i*)
- àwritelnÇ('iisnowequalto',i);
- àEND.Ç
-
- (üProgram3Ç)
-
- Inthelastexample,thereare4variablesdeclaredas3
- differenttypes.
- ü
- ëINTEGERÇ:Thesearethevariablesthattakewholenumbers
- (integers).Allowedvaluesareintherangeof
- -32768and32767inclusive.Ifaninteger
- overflowsthesevalues,youmustdeclareitas
- typeREAL.
-
- ëREALüÇ:Thesearethevariablesthattakerealnumber
- values.Thesevaluesareintherangeof1.0*10
- exponent-38and1.0*10exponent38.InPASCAL,the
- waytorepresentthemantissaandexponentisthe
- following:3.8E2
-
- whichmeans3.8*10tothesecondpower,or380.It
- isalsopossibletoassignavaluetoavariable
- withoutmantissaorexponent.Thus,thesetwo
- examplesarevalid:
-
- number:=3.8E2;
- number:=380.0;
-
- Itisalsousefultospecifythatwecanalso
- formattheoutputofarealnumberbyindicating
- thenumberofdigitsanddecimalswantedfor
- output.Example:
-
- àPROGRAMÇdisplay(àOUTPUTÇ)
- àVARÇ
- number:àREALÇ;
- àBEGINÇ
- number:=3.1234;
- àWRITELNÇ(number:6:2);
- àEND.Ç
-
- (üProgram4Ç)
-
- TheàWRITELNÇ(number:6:2)indicatesthatwewanttowritethe
- valueofNUMBERwithin6placesand2digitsofdecimals.The
- outputwillbelikethis:
-
- ^^3.12
-
- ëCHARÇ:Thistypeofvariablecanbeassignedacharacter.
- Thecharactermaybealetter,adigit,orany
- otherlike%,@,&,etc...Herearesomeassignment
- examples:
-
- charact:='e';
- letter:='Q';
- number:='4';
- a:='a';
- c:='^';
-
- TherearealsootherTYPESwhichwewillexaminein
- thenextlesson.
-
- ëOPERATORSÇ:
- ---ONINTEGERS--
-
- ADDITION(+),example:a:=3+4;aisequalto7and
- mustbedeclaredas
- anàINTEGER
- Ç
- SUBTRACTION(-),example:a:=3-4;aisequalto-1and
- mustbedeclaredas
- anàINTEGERÇ
- MULTIPLICATION(*),
- example:a:=3*4,aisequalto12and
- mustbedeclaredas
- anàINTEGERÇ
-
- DIVISION(/),example:a:=3/4,aisequalto0.75
- andmustbedeclared
- asaàREALÇnumber.
- INTEGERDIVISION(DIV),
- example:a:=3DIV4,Theresultisthe
- integerpartofthe
- division,ex.:a:=3
- DIV4,isequalto0
- andmustbedeclared
- asanàINTEGERÇ.
- REMAINDEROFDIVISION(MOD),
- example:a:=3MOD4,Theresultisthe
- remainderofthe
- operation,ex.:a:=3
- MOD4,isequalto3
- andmustbedeclared
- asanàINTEGERÇ.
-
- --ONREALNUMBERS--
-
- ADDITION(+),example:a:=4.48+2.0;
- aequals6.48andisdeclared
- asaàREALÇnumber.
- SUBTRACTION(-),example:a:=4.48-2.0;
- aequals2.48andisdeclared
- asaàREALÇnumber.
- MULTIPLICATION(*),example:a:=4.48*2.0;
- aequals8.96andisdeclared
- asaàREALÇnumber.
- DIVISION(/),example:a:=4.48/2.0;
- aequals2.24andisdeclared
- asaàREALÇnumber.
-
- ëTHESTANDARDARITHMETICFUNCTIONSÇ
- =================================
-
- +-----------------------------------------------------------+
- |PASCALfunction|meaning|typeb|typea|
- +------------------------+--------------+---------+---------+
- |a:=äABSÇ(b)|unsigned|integer|sameas|
- ||valueof(b)|orreal|b|
- |a:=äSQRÇ(b)|squareof|integer|sameas|
- ||(b)|orreal|b|
- |a:=äSINÇ(b) |sineof(b)|integer|real|
- | | |orreal||
- |a:=äCOSÇ(b) |cosine |integer|real|
- | |of(b) |orreal| |
- |a:=äARCTANÇ(b) |arctangent |integer|real|
- | |of(b) |orreal||
- |a:=äSQRTÇ(b) |squareroot|integer|real|
- | |of(b) |orreal||
- |a:=äLNÇ(b) |naturallog|integer|real|
- | |of(b) |orreal||
- |a:=äEXPÇ(b) |exponentof|integer|real|
- ||(b) |orreal||
- |a:=äTRUNCÇ(b)|integerpart|real|integer|
- ||of(b) |||
- |a:=äROUNDÇ(b)|truncated|real|integer|
- ||partof(b)|||
- +------------------------+--------------+---------+---------+
-
- (üFigure6Ç)
-
-
- ëREADandREADLNÇ
-
- WehaveseenwithàWRITEÇandàWRITELNÇ,howtodisplaythe
- outputonscreen.Theonlythingisthatwhatweweredisplaying
- wasalreadyincludedintheprogram.Withwhatweknow,ifwe
- wantedtowriteaprogramthataverages5numbers,thisistheway
- wewouldproceed:
-
- àPROGRAMÇaverage(àOUTPUTÇ);
- àVARÇ
- ave:àREALÇ;
- àBEGINÇ
- ave:=(70.0+75.5+55.8+60.3+85.0)/5;
- àWRITELNÇ('Theaverageis',ave:5:1);
- àEND.Ç
-
- (üProgram5Ç)
-
- Thismini-programeffectivelyaveragesfivenumbers,but
- becomesinefficientwhenweneedtochangethevaluestobe
- averaged,theonlywaybeingtomodifytheprogram.Butthereisa
- solution:
-
- àPROGRAMÇaverage2(àINPUT,OUTPUTÇ);
- àVARÇ
- nb1,nb2,nb3,nb4,nb5:àREALÇ;
- (*5numberstoaverage*)
- ave:àREALÇ;(*average*)
- àBEGINÇ
- àREADLNÇ(nb1,nb2,nb3,nb4,nb5);(*Keyboardentry*)
- ave:=(nb1+nb2+nb3+nb4+nb5)/5;(*calculateaverage*)
- àWRITELNÇ('Theaverageis',ave:5:1);
- àEND.Ç
-
- (üProgram6Ç)
-
- WhatwenoticehereisàREADLNÇ.Thisinstructionallowsus
- toenterthevaluesfromthekeyboard,aboutinthesamemanneras
- withINPUTinBASIC.Fromthenon,itispossibletoreusethe
- programwithoutmodifyingit.
-
- TherelationbetweenàREADÇandàREADLNÇisthesameaswithàWRITEÇ
- andàWRITELNÇ.
-
- Syntax:àREADÇoràREADLNÇ
- |
- (
- |---⇦---+
- Variable|
- |---⇨---+
- )
-
- (üFigure7Ç)
-
- ëTheìFOR...DOëLoopÇ
-
- Let'suseouraveragingprogramagain.It'snotvery
- usefulthewaywewroteitbecauseitonlyallows5numberstobe
- averaged.Whatifwewanttoaverage6numbers?Doitoverand
- overagain?
-
- OnesolutiontothisproblemistousetheàFOR...DOÇ
- instructions.
-
- àPROGRAMÇaverage3(àINPUT,OUTPUTÇ);
- àVARÇ
- nbentries(*numberofentriesneeded*)
- i:àINTEGERÇ;(*loopcounter*)
- nbr,(*valuereadfromkeyboard*)
- total,(*totalofthevalues*)
- ave:àREALÇ;(*calculatedaverage*)
- àBEGINÇ
- àWRITEÇ('Inputnumberofentriesneeded');
- (*promptstheuserforinput*)
- total:=0;(*initializetotaltozero*)
- àREADLNÇ(nbentries);(*readsfromthekeyboard*)
- (*thenumberofentriesneeded*)
- àFORÇi:=1àTOÇnbentriesàDOÇ
- (*foreachentry,repeatthefollowing*)
- àBEGINÇ(*startstheloop*)
- àREADLNÇ(nbr);(*readsanumber*)
- total:=total+nbr;(*addnumbertototal*)
- àENDÇ;(*fori*)(*endstheloop*)
- ave:=total/5;(*calculatesaverage*)
- àEND.Ç
-
- (üProgram7Ç)
-
- IfyouunderstandthemeaningofàFOR...DOÇ(whichisthe
- sameasFOR...NEXTinBASIC)younoticethatwhatiscontained
- betweentheàBEGINÇandtheàENDÇoftheàFORÇloopwillexecuteas
- longastheloopcounter(representedbyi)hasnotexceededthe
- maximumvalue.TheàBEGINÇandàENDÇinstructionsareneededonly
- whenthereismorethanoneinstructioncontainedintheàFOR...
- DOÇloop.
-
- ThecountermaybeoftypeàINTEGERÇ,àREALÇ,àCHARÇ,or
- scalar(tobeexaminedinafuturetutorial).It'salsopossible
- todecrementthecounterinsteadofincrementingbyreplacingthe
- àTOÇwithaàDOWNTOÇ.
-
- Syntax: àFORÇ
- |
- variablecounter
- |
- :=
- |
- expression
- |
- àTOÇoràDOWNTOÇ
- |
- expression
- |
- àDOÇ
- |
- instruction
-
-
- Thenextlessonwilldealwithmatrixvariables.Wewillalso
- studyothertypesofvariables,otherinstructions,andstart
- explainingPROCEDURESandFUNCTIONS.
-
- üSergeVaillancourt
- March1986Ç
-
-