Download - Tutorial_ for Loops and While Loops - National Instruments

Transcript
  • Tutorial:ForLoopsandWhileLoopsPublishDate:Apr01,2015

    OverviewThistutorialexploressomeofthebasicfunctionsandusesofForLoopsandWhileLoops.YouwilllearnhowtocreateForLoopsandWhileLoopsandwhentheappropriatetimewouldbetousetheminyourprogram.

    TableofContents

    AwhileloopisacontrolflowstatementyouusetoexecuteablockofthesubdiagramcoderepeatedlyuntilagivenBooleanconditionismet.First,youexecutethecodewithinthesubdiagram,andthentheconditionalterminalisevaluated.Unlikeaforloop,awhileloopdoesnothaveasetiterationcountthus,awhileloopexecutesindefinitelyiftheconditionneveroccurs.

    Aforloopisacontrolflowstatementyouusetoexecuteablockofthesubdiagramcodeasetnumberoftimes,butawhileloopstopsexecutingthesubdiagramonlyifthevalueattheconditionalterminalexists.

    BuildingaWhileLoop

    1.OpenanewVI.YoucanopenablankVIbyselectingFileNewVI2.IftheFunctionspaletteisnotvisible,rightclickanyblankspaceontheblockdiagramtodisplayatemporaryversionofthepalette.ClickthethumbtackintheupperleftcorneroftheFunctionspalettetopinthepalettesoitisnolongertemporary.3.SelectthewhileloopfromtheStructurespaletteundertheFunctionspalette.

  • 4.Usethecursortodragaselectionrectanglearoundthesectionoftheblockdiagramyouwanttorepeat.

    5.Whenyoureleasethemousebutton,awhileloopboundaryenclosesthesectionyouhaveselected.

    6.PlaceaStopbuttononthefrontpanel.YoucanfindthisunderControlsPaletteBooleanStop.

    7.AddtheStopbuttonfromtheblockdiagramtothewhileloopbydragginganddroppingitinsidethewhileloop.

    8.Theconditionalterminal,shownbelow,defineswhentheloopstops.Therearetwosettingsfortheconditionalterminal:ContinueifTrueandStopifTrue.WhensettoContinueifTrue,thewhilelooprunsonlyifaBooleanvalueoftrueissenttotheterminal.IftheconditionalterminalissettoStopifTrue,andaBooleanvalueoftrueissenttotheconditionalterminal,theloophaltsexecution.

    StopifTrue

  • ContinueifTrue

    9.ToswitchtheconditionalterminalbetweenContinueifTrueandStopifTrue,rightclickontheconditionalterminalandcheckthecorrespondingsetting.

    10.WiretheStopbuttontotheconditionalterminalsothatyoucancontroltheexecutionofthewhileloop.WhentheStopbuttonispressed,atruevalueispassedtotheconditionalterminalcausingthewhilelooptostopexecution.YoucanwireanyBooleandatatotheconditionalterminaltocontroltheexecutionofawhileloop.

    11.Theiterationterminalisanoutputterminalthatcontainsthenumberofcompletediterations.Theiterationcountalwaysstartsatzero.Duringthefirstiteration,theiterationterminalreturns0.

    12.YouhavejustcreatedasimplewhileloopthatgeneratesrandomnumbersanddisplaysthemuntiltheStopbuttonispressed.

    StructureTunnels

    Usestructuretunnelstofeeddataintoandoutofstructureslikethewhileloop.Ifyouwanttosenddataintoyourwhileloop,youneedtocreatestructuretunnels.DatasentintotheWhileLoopissentonlyonthefirstiteration,anddatasentoutofthewhileloopissentonlyafterthelastiteration.Nowcreateastructuretunneltodisplaythenumberofiterationsexecuted.

    1.Createanumericindicatoronthefrontpanel.

  • 2.Dragadatawirefromtheiterationcounterintotheborderofthewhilelooptocreateastructuretunnel.

    3.Thetunnelappearsasasolidblockontheborderofthewhileloop.Theblockisthecolorofthedatatypewiredtothetunnel.Datapassoutofaloopaftertheloopterminates.Whenatunnelpassesdataintoaloop,theloopexecutesonlyafterdataarriveatthetunnel.

    4.Dragadatawirefromthestructuretunnelintothenumericindicatortopassthenumberofiterationstoyourdisplay.

    5.Inthefollowingblockdiagram,theiterationterminalisconnectedtoatunnel.Thevalueinthetunneldoesnotgetpassedtotheiterationindicatoruntilthewhileloopfinishesexecuting.

    ManipulatingtheConditionalTerminal

    ByusingBooleanfunctions,youcanimplementmultipleconditionstoaffectyourwhileloopconditionalterminal.YoucanuseanorfunctiontocompareanerrorwirestatusandaStopbuttoncontrolsothatifeitherisTRUE,theconditionalterminalreceivesaTRUEsignal,andthewhileloopstops.

    1.Createawhileloopasoutlinedpreviously.

    2.PlaceanErrorIn3D.ctlcontrolonthefrontpanel.YoucanfindthisunderControlsPaletteArray,Matrix&ClusterErrorIn3D.ctl.

  • 3.RemovetheconnectionwirebetweentheStopbuttonandtheconditionalterminal.4.MakesuretheErrorIn3D.ctlisoutsideofthewhileloop.5.PlaceaSimpleErrorHandleroutsideofthewhileloop.YoucanfindthisunderFunctionsPaletteDialog&UserInterfaceSimpleErrorHandler.vi.Thisfunctionreportsanyerrorsencounteredwhencalled.

    6.LinktheerrorincontrolwiththeSimpleErrorHandlerthroughthewhileloopviastructuretunnels.Dragawirefromtheoutputoftheerrorincontroltotheborderofthewhileloop,andthendragtheerrorwirefromthetunnelcreatedtotheoppositeborderofthewhileloopandintotheinputoftheSimpleErrorHandler.

    7.PlaceanUnbundleByNamefunctioninsideofthewhileloop.YoucanfindthisunderFunctionsPaletteProgrammingCluster,Class,&Variant.ThisfunctionseparatestheBooleanportionoftheerrordatawire.

    8.Placeanorfunctioninsideofthewhileloop.YoucanfindthisunderFunctions

  • PaletteProgrammingBoolean.

    9.Linkthedatawiresasshownbelow.ThissimpleVIstopsexecutionofthewhileloopifeithertheStopbuttonispressedoranerrorisdetectedinsideofthewhileloop.

    BuildingaForLoop

    1.OpenanewVI.YoucanopenablankVIbyselectingFileNewVI.2.IftheFunctionspaletteisnotvisible,rightclickanyblankspaceontheblockdiagramtodisplayatemporaryversionoftheFunctionspalette.ClickthethumbtackintheupperleftcorneroftheFunctionspalettetopinthepalettesoitisnolongertemporary.3.SelecttheforloopfromtheStructurespaletteundertheFunctionspalette.

    4.Youalsocanplaceawhileloopontheblockdiagram.RightclicktheborderofthewhileloopandselectReplacewithForLoopfromtheshortcutmenutochangeawhilelooptoaforloop.

  • 5.Aforloopcontainsacountterminal.Thecountterminaldictateshowmanytimesthesubdiagramisexecuted.6.RightclickonthecountterminalandCreateConstanttolinkthecountterminalwithanumericvalue.

    7.Byinserting100intothenumericconstant,theforloopexecutes100timesbeforestopping.

    8.Theiterationterminal,shownasfollows,isanoutputterminalthatcontainsthenumberofcompletediterations.Theexamplebelowwouldupdateanindicatoronthefrontpanelwiththecurrentiterationnumber.

    AddingaConditionalTerminaltoaForLoop

    Ifnecessary,youcanaddaconditionalterminaltoconfigureaforlooptostopwhenaBooleanconditionoranerroroccurs.1.RightclickontheedgeoftheforloopandselectConditionalTerminal.

    2.Aforloopwithaconditionalterminalexecutesuntiltheconditionoccursoruntilalliterationsarecomplete,whicheverhappensfirst.3.Forloopsyouconfigureforaconditionalexithavearedglyphinthecountterminalaswellasaconditionalterminalinthelowerrightcorner.

  • 4.Afteryouconfiguretheforlooptoexitconditionally,theloopappearssimilartothefigurebelow.Thefollowingforloopgeneratesarandomnumbereveryseconduntil100secondshaspassedortheuserclickstheStopbutton.

    Formoreinformationontheusesofforloopsandwhileloops,refertotheContextHelp.YoucanaccessContextHelpfromHelpShowContextHelporusingtheshortcut.

    VideoExerciseLoopsModulesHomeFIRSTCommunity