Professional Documents
Culture Documents
PublishDate:Apr01,2015
Overview
ThistutorialexploressomeofthebasicfunctionsandusesofForLoopsandWhileLoops.
YouwilllearnhowtocreateForLoopsandWhileLoopsandwhentheappropriatetime
wouldbetousetheminyourprogram.
TableofContents
Awhileloopisacontrolflowstatementyouusetoexecuteablockofthesubdiagramcode
repeatedlyuntilagivenBooleanconditionismet.First,youexecutethecodewithinthe
subdiagram,andthentheconditionalterminalisevaluated.Unlikeaforloop,awhileloop
doesnothaveasetiterationcountthus,awhileloopexecutesindefinitelyifthecondition
neveroccurs.
Aforloopisacontrolflowstatementyouusetoexecuteablockofthesubdiagramcodea
setnumberoftimes,butawhileloopstopsexecutingthesubdiagramonlyifthevalueatthe
conditionalterminalexists.
BuildingaWhileLoop
1.OpenanewVI.YoucanopenablankVIbyselectingFileNewVI
2.IftheFunctionspaletteisnotvisible,rightclickanyblankspaceontheblockdiagramto
displayatemporaryversionofthepalette.Clickthethumbtackintheupperleftcornerofthe
Functionspalettetopinthepalettesoitisnolongertemporary.
3.SelectthewhileloopfromtheStructurespaletteundertheFunctionspalette.
4.Usethecursortodragaselectionrectanglearoundthesectionoftheblockdiagramyou
wanttorepeat.
5.Whenyoureleasethemousebutton,awhileloopboundaryenclosesthesectionyou
haveselected.
6.PlaceaStopbuttononthefrontpanel.YoucanfindthisunderControls
PaletteBooleanStop.
7.AddtheStopbuttonfromtheblockdiagramtothewhileloopbydragginganddroppingit
insidethewhileloop.
8.Theconditionalterminal,shownbelow,defineswhentheloopstops.Therearetwo
settingsfortheconditionalterminal:ContinueifTrueandStopifTrue.WhensettoContinue
ifTrue,thewhilelooprunsonlyifaBooleanvalueoftrueissenttotheterminal.Ifthe
conditionalterminalissettoStopifTrue,andaBooleanvalueoftrueissenttothe
conditionalterminal,theloophaltsexecution.
StopifTrue
ContinueifTrue
9.ToswitchtheconditionalterminalbetweenContinueifTrueandStopifTrue,rightclickon
theconditionalterminalandcheckthecorrespondingsetting.
10.WiretheStopbuttontotheconditionalterminalsothatyoucancontroltheexecutionof
thewhileloop.WhentheStopbuttonispressed,atruevalueispassedtotheconditional
terminalcausingthewhilelooptostopexecution.YoucanwireanyBooleandatatothe
conditionalterminaltocontroltheexecutionofawhileloop.
11.Theiterationterminalisanoutputterminalthatcontainsthenumberofcompleted
iterations.Theiterationcountalwaysstartsatzero.Duringthefirstiteration,theiteration
terminalreturns0.
12.Youhavejustcreatedasimplewhileloopthatgeneratesrandomnumbersanddisplays
themuntiltheStopbuttonispressed.
StructureTunnels
Usestructuretunnelstofeeddataintoandoutofstructureslikethewhileloop.Ifyouwantto
senddataintoyourwhileloop,youneedtocreatestructuretunnels.DatasentintotheWhile
Loopissentonlyonthefirstiteration,anddatasentoutofthewhileloopissentonlyafter
thelastiteration.Nowcreateastructuretunneltodisplaythenumberofiterationsexecuted.
1.Createanumericindicatoronthefrontpanel.
2.Dragadatawirefromtheiterationcounterintotheborderofthewhilelooptocreatea
structuretunnel.
3.Thetunnelappearsasasolidblockontheborderofthewhileloop.Theblockisthecolor
ofthedatatypewiredtothetunnel.Datapassoutofaloopaftertheloopterminates.When
atunnelpassesdataintoaloop,theloopexecutesonlyafterdataarriveatthetunnel.
4.Dragadatawirefromthestructuretunnelintothenumericindicatortopassthenumberof
iterationstoyourdisplay.
5.Inthefollowingblockdiagram,theiterationterminalisconnectedtoatunnel.Thevaluein
thetunneldoesnotgetpassedtotheiterationindicatoruntilthewhileloopfinishes
executing.
ManipulatingtheConditionalTerminal
ByusingBooleanfunctions,youcanimplementmultipleconditionstoaffectyourwhileloop
conditionalterminal.YoucanuseanorfunctiontocompareanerrorwirestatusandaStop
buttoncontrolsothatifeitherisTRUE,theconditionalterminalreceivesaTRUEsignal,and
thewhileloopstops.
1.Createawhileloopasoutlinedpreviously.
2.PlaceanErrorIn3D.ctlcontrolonthefrontpanel.YoucanfindthisunderControls
PaletteArray,Matrix&ClusterErrorIn3D.ctl.
3.RemovetheconnectionwirebetweentheStopbuttonandtheconditionalterminal.
4.MakesuretheErrorIn3D.ctlisoutsideofthewhileloop.
5.PlaceaSimpleErrorHandleroutsideofthewhileloop.YoucanfindthisunderFunctions
PaletteDialog&UserInterfaceSimpleErrorHandler.vi.Thisfunctionreportsanyerrors
encounteredwhencalled.
6.LinktheerrorincontrolwiththeSimpleErrorHandlerthroughthewhileloopviastructure
tunnels.Dragawirefromtheoutputoftheerrorincontroltotheborderofthewhileloop,
andthendragtheerrorwirefromthetunnelcreatedtotheoppositeborderofthewhileloop
andintotheinputoftheSimpleErrorHandler.
7.PlaceanUnbundleByNamefunctioninsideofthewhileloop.Youcanfindthisunder
FunctionsPaletteProgrammingCluster,Class,&Variant.Thisfunctionseparatesthe
Booleanportionoftheerrordatawire.
8.Placeanorfunctioninsideofthewhileloop.YoucanfindthisunderFunctions
PaletteProgrammingBoolean.
9.Linkthedatawiresasshownbelow.ThissimpleVIstopsexecutionofthewhileloopif
eithertheStopbuttonispressedoranerrorisdetectedinsideofthewhileloop.
BuildingaForLoop
1.OpenanewVI.YoucanopenablankVIbyselectingFileNewVI.
2.IftheFunctionspaletteisnotvisible,rightclickanyblankspaceontheblockdiagramto
displayatemporaryversionoftheFunctionspalette.Clickthethumbtackintheupperleft
corneroftheFunctionspalettetopinthepalettesoitisnolongertemporary.
3.SelecttheforloopfromtheStructurespaletteundertheFunctionspalette.
4.Youalsocanplaceawhileloopontheblockdiagram.Rightclicktheborderofthewhile
loopandselectReplacewithForLoopfromtheshortcutmenutochangeawhilelooptoa
forloop.
5.Aforloopcontainsacountterminal.Thecountterminaldictateshowmanytimesthe
subdiagramisexecuted.
6.RightclickonthecountterminalandCreateConstanttolinkthecountterminalwitha
numericvalue.
7.Byinserting100intothenumericconstant,theforloopexecutes100timesbefore
stopping.
8.Theiterationterminal,shownasfollows,isanoutputterminalthatcontainsthenumberof
completediterations.Theexamplebelowwouldupdateanindicatoronthefrontpanelwith
thecurrentiterationnumber.
AddingaConditionalTerminaltoaForLoop
Ifnecessary,youcanaddaconditionalterminaltoconfigureaforlooptostopwhena
Booleanconditionoranerroroccurs.
1.RightclickontheedgeoftheforloopandselectConditionalTerminal.
2.Aforloopwithaconditionalterminalexecutesuntiltheconditionoccursoruntilall
iterationsarecomplete,whicheverhappensfirst.
3.Forloopsyouconfigureforaconditionalexithavearedglyphinthecountterminalaswell
asaconditionalterminalinthelowerrightcorner.
4.Afteryouconfiguretheforlooptoexitconditionally,theloopappearssimilartothefigure
below.Thefollowingforloopgeneratesarandomnumbereveryseconduntil100seconds
haspassedortheuserclickstheStopbutton.
Formoreinformationontheusesofforloopsandwhileloops,refertotheContextHelp.You
canaccessContextHelpfromHelpShowContextHelporusingtheshortcut<CtrlH>.
VideoExerciseLoopsModulesHomeFIRSTCommunity