Once jaIlIU was de pressed the operation that we specified was performed immediately This is called the IMMEDIATE or CALCULATOR mode But to accomplish anything significant we must be able to have the computer operate with more than single line state ID: 50173
Download Pdf The PPT/PDF document "Up to now weve performed some simple ope..." is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Uptonowwe'veperformedsomesimpleoperationsbyenteringasinglelineofinstructionsintothecomputer.Once.:j:aIlI~U.wasde-pressed,theoperationthatwespecifiedwasperformedimmediately.ThisiscalledtheIMMEDIATEorCALCULATORmode.Buttoaccomplishanythingsignificant,wemustbeabletohavethecomputeroperatewithmorethanasinglelinestatement.AnumberofstatementscombinedtogetheriscalledaPROGRAMandallowsyoutousethefullpoweroftheCommodore64.ToseehoweasyitistowriteyourfirstCommodore64program,trythis:Clearthescreenbyholdingthe_key,andthendepressingtheItJ.~fj:[.],',I:Ikey.TypeNEWandpress.~j:alll=!/..(Thisjustclearsoutanynumbersthatmighthavebeenleftinthecomputerfromyourexperimenting.)Nowtypethefollowingexactlyasshown(Remembertohit.:j:alll~j/.aftereachline)f1121?"CONNODORE64"I2121GOTO1121.Now,typeRUNandhit.~j:alll:j/.-watchwhathappens.YourscreenwillcomealivewithCOMMODORE64.Afteryou'vefinishedwatchingthedisplay,hit.~m/'I.'t(ll:ltostoptheprogram.Anumberofimportantconceptswereintroducedinthisshortpro-gramthatarethebasisforallprogramming.Noticethathereweprecededeachstatementwithanumber.ThisLINEnumbertellsthecomputerinwhatordertoworkwitheachstate-ment.Thesenumbersarealsoareferencepoint,incasetheprogramneedstogetbacktoaparticularline.linenumberscanbeanywholenumber(integer)valuebetween0-63,999.1~PRINT"COMMODORE64"rLSTATEMENTLINENUMBER32I Itisgoodprogrammingpracticetonumberlinesinincrementsof10-incaseyouneedtoinsertsomestatementslateron.BesidesPRINT,ourprogramalsousedanotherBASICcommand,GOTO.Thisinstructsthecomputertogodirectlytoaparticularlineandperformit,thencontinuefromthatpoint.[HI'PRINT"COMMODORE64"2"GOTO1"Inourexample,theprogramprintsthemessageinline10,goestothenextline(20),whichinstructsittogobacktoline10andprintthemessageoveragain.Thenthecyclerepeats.Sincewedidn'tgivethecomputerawayoutofthisloop,theprogramwillcycleendlessly,untilwephysicallystopitwiththe.:ml'u.UIkey.Onceyou'vestoppedtheprogram,type:LIST.Yourprogramwillbedisplayed,intact,becauseit'sstillinthecomputer'smemory.Notice,too,thatthecomputerconvertedthe?intoPRINTforyou.Theprogramcannowbechanged,saved,orrunagain.Anotherimportantdifferencebetweentypingsomethingintheim-mediatemodeandwritingaprogramisthatonceyouexecuteandclearthescreenofanimmediatestatement,it'slost.However,youcanalwaysgetaprogrambackbyjusttypingLIST.Bytheway,whenitcomestoabbreviationsdon'tforgetthatthecomputermayrunoutofspaceonalineifyouusetoomany.33 EDITINGTIPSIfyoumakeamistakeonaline,yojuhaveanumberofeditingoptions.1.Youcanretypealineanytime,andthecomputerwillautomaticallysubstitutethenewlinefortheoldone.2.Anunwantedlinecanbeerasedbysimplytypingthelinenumberand.II.IIIII~..3.Youcanalsoeasilyeditanexistingline,usingthecursorkeysandeditingkeys.Supposeyoumadeatypingmistakeinalineoftheexample.Tocorrectitwithoutretypingtheentireline,trythis:TypeLIST,thenusingthe..and11..m."I'.~1keystogethermovethecursorupuntilitispositionedonthelinethatneedstobechanged.Now,usethecursor-rightkeytomovethecursortothecharacteryo'uwanttochange,typingthechangeovertheoldcharacter.Nowhit.~I:lIII:I~.andthecorrectedlinewillreplacetheoldone.Ifyouneedmorespaceontheline,positionthecursorwherethespaceisneededandhitIDIIiIand.1~1o"t1J.4I'I~.atthesametimeandaspacewillopenup.Nowjusttypeintheadditionalinformationandhit.:I:lIIIII~..Likewise,youcandeleteunwantedcharactersbyplacingthecursortotherightoftheunwantedcharacterandhittingthe.1~Io"tIJ.4I'I~.key.Toverifythatchangeswereentered,typeLISTagain,andthecor-rectedprogramwillbedisplayed!Andlinesdon'thavetobeenteredinnumericalorder.Thecomputerwillautomaticallyplacetheminthepropersequence.Tryeditingoursampleprogramonpage33bychangingline10andaddingacommatotheendoftheline.ThenRUNtheprogramagain.~DON'TFORGETTOMOVETHECURSORPASTLINE20BEFOREYOURUNTHEPROGRAM.HIIPRINT"COMMODORE",VARIABLESVariablesaresomeofthemostusedfeaturesofanyprogramminglanguage,becausevariablescanrepresentmuchmoreinformationinthecomputer.Understandinghowvariablesoperatewillmakecomput-ingeasierandallowustoaccomplishfeatsthatwouldnotbepossibleotherwise.34 COMMODORECOMMODORECOMMODORCOMMODORECOMMODORECOMMODORECOMMODORECOMMODORECOMMODORECOMMODORECOMMODOREBREAKIN10READY.COMMODORECOMMODORECOMMODORECOMMODORECOMMODORECOMMODORE~M~O~ECOMMODORECOMMODORECOMMODORECOMMODORECOMMODORECOMMODORECOMMODORECOMMODORECOMMODORECOMMODORECOMMODORECOMMODORECOMMODORECOMMODORECOMMODORECOMMODOREICOMMODORECOMM06oRECOMM06oRECOMMODORECOMMODORECOMMODORECOM~J~ORECOMMODORECOMMO~ORECOMMODORECOMMOBORECOMMOIDOREImagineanumberofboxeswithinthecomputerthatcaneachholdanumberorastringoftextcharacters.Eachoftheseboxesistobelabeledwithanamethatwechoose.Thatnameiscalledavariableandrepresentstheinformationintherespectivebox.Forexample,ifwesay:113X%=1520X=23.5313X$="THESUMOFX%+X="Thecomputermightrepresentthevariableslikethis:X%15X23.5X$THESUMOFX%+XAvariablenamerepresentsthebox,ormemorylocation,wherethecurrentvalueofthevariableisstored.Asyoucansee,wecanassigneitheranintegernumber,floatingpointnumber,oratextstringtoavariable.The%symbolfollowingavariablenameindicatesthevariablewillrepresentanintegernumber.Thefollowingarevalidintegervariablenames:35 A%X%Al%NM%The'$'followingthevariablenameindicatesthevariablewillrepre-sentatextstring.Thefollowingareexamplesofstringvariables:A$Floatingpointvariablesfollowthesameformat,withthetypeindi-cator:AlXMIInassigninganametoavariablethereareafewthingstokeepinmind.First,avariablenamecanhaveoneortwocharacters.ThefirstcharactermustbeanalphabeticcharacterfromAtoZ;thesecondcharactercanbeeitheralphabeticornumeric(intherange0to9).Athirdcharactercanbeincludedtoindicatethetypeofvariable(integerortextstring),%or$.Youcanusevariablenameshavingmorethantwoalphabeticcharacters,butonlythefirsttwoarerecognizedbythecomputer.SoPAandPARTNOarethesameandwouldrefertothesamevariablebox.Thelastruleforvariablenamesissimple:theycan'tcontainanyBASICkeywords(reservedwords)suchasGOTO,RUN,etc.ReferbacktoAppendixDforacompletelistofBASICreservedwords.Toseehowvariablescanbeputtowork,typeinthecompletepro-gramthatweintroducedearlierandRUNit.Remembertohit.:j:lIlI:U.aftereachlineintheprogram.36 Ifyoudideverythingasshown,youshouldgetthefollowingresultprintedonthescreen.We'veputtogetherallthetrickslearnedsofartoformatthedisplayasyouseeitandprintthesumofthetwovariables.Inlines10and20weassignedanintegervaluetoX%andassignedafloatingpointvaluetoX.Thisputsthenumberassociatedwiththevari-ableinitsbox.Inline30,weassignedatextstringtoX$.Line40combinesthetwotypesofPRINTstatementstoprintamessageandtheactualvalueofX%andX.Line50printsthetextstringassignedtoX$andthesumofX%andX.NotethateventhoughXisusedaspartofeachvariablename,theidentifiers%and$makeX%,X,andX$unique,thusrepresentingthreedistinctvariables.Butvariablesaremuchmorepowerful.Ifyouchangetheirvalue,thenewvaluereplacestheoriginalvalueinthesamebox.Thisallowsyoutowriteastatementlike:X=X+lThiswouldneverbeacceptedinnormalalgebra,butisoneofthemostusedconceptsinprogramming.Itmeans:takethecurrentvalueofX,addonetoitandplacethenewsumintotheboxrepresentingX.IF...THENArmedwiththeabilitytoeasilyupdatethevalueofvariables,wecannowtryaprogramsuchas:37 Whatwe'vedoneisintroducetwonewBASICcommands,andpro-videdsomecontroloverourrunawaylittleprintprogramintroducedatthestartofthischapter.IF...THENaddssomelogictotheprogram.ItsaysIFaconditionholdstrueTHENdosomething.IFtheconditionnolongerholdstrue,THENdothenextlineintheprogram.AnumberofconditionscanbesetupinusinganIF...THENstate-ment:SYMBOL===Theuseofanyoneofthesepowerful.1"CT="[2"?"COMMODORE64"30'CT=CT+14"IFCT5THEN2"15"ENDMEANINGLessThanGreaterThanEqualToNotEqualToGreaterThanorEqualToLessThanorEqualToconditionsissimple,yetsurprisingly38 Inthesampleprogram,we'vesetupa"loop"thathassomecon-straintsplacedonitbysaying:IFavalueislessthansomenumberTHENdosomething.Line10setsCT(CounT)equaltoO.Line20printsourmessage.Line30addsonetothevariableCT.Thislinecountshowmanytimeswedotheloop.Eachtimetheloopisexecuted,CTgoesupbyone.Line40isourcontrolline.IfCTislessthan5,meaningwe'veexe-cutedthelooplessthan5times,theprogramgoesbacktoline20andprintsagain.WhenCTbecomesequalto5-indicating5COMMODORE64'swereprinted-theprogramgoestoline50,whichsignalstoENDtheprogram.Trytheprogramandseewhatwemean.BychangingtheCTlimitinline40youcanhaveanynumberoflinesprinted.IF...THENhasamultitudeofotheruses,whichwe'llseeinfutureexamples.FOR...NEXTLOOPSThereisasimpler,andpreferredwaytoaccomplishwhatwedidinthepreviousexamplebyusingaFOR...NEXTloop.Considerthefollowing:Asyoucansee,theprogramhasbecomemuchsmallerandmoredirect.CTstartsat1inline10.Then,line20doessomeprinting.InLine3039 CTisincrementedby1.TheNEXTstatementinline30automaticallysendstheprogrambacktoline10wheretheFORpartoftheFOR...NEXTstatementislocated.ThisprocesswillcontinueuntilCTreachesthelimityouentered.ThevariableusedinaFOR...NEXTloopcanbeincrementedbysmalleramountsthan1,ifneeded.Trythis:Ifyouenterandrunthisprogram,you'llseethenumbersfrom1to10,by.5,printedacrossthedisplay.Allwe'redoinghereisprintingthevaluesthatNBassumesasitgoesthroughtheloop.Youcanevenspecifywhetherthevariableisincreasingordecreas-ing.Substitutethefollowingforline10:10FORNB=10to1STEP-.5andwatchtheoppositeoccur,asNBgoesfrom10to1indescendingorder.40