Loop - Syntax & Usage - AutoHotkey

文章推薦指數: 80 %
投票人數:10人

The Loop statement performs a series of code lines repeatedly: either the specified number of times or until a Break statement is encountered. Loop(normal) Performsaseriesofcommandsrepeatedly:eitherthespecifiednumberoftimesoruntilbreakisencountered. Loop,Count Parameters Count Howmanytimes(iterations)toperformtheloop.Ifomitted,theLoopcontinuesindefinitelyuntilabreakorreturnisencountered. IfCountisavariablereferencesuchas%ItemCount%,theloopisskippedentirelywheneverthevariableisblankorcontainsanumberlessthan1. Duetotheneedtosupportfile-patternloops,Countcannotbeanexpression.However,aswithallnon-expressionparameters,anexpressioncanbeforciblyusedbyprecedingitwitha%andaspace.Forexample:Loop%Count+1.Insuchcases,theexpressionisevaluatedonlyonce,rightbeforetheloopbegins. Remarks Theloopcommandisusuallyfollowedbyablock,whichisacollectionofstatementsthatformthebodyoftheloop.However,aloopwithonlyasinglestatementdoesnotrequireablock(an"if"andits"else"countasasinglestatementforthispurpose). Acommonuseofthiscommandisaninfiniteloopthatusesthebreakcommandsomewhereintheloop'sbodytodeterminewhentostoptheloop. Theuseofbreakandcontinueinsidealoopareencouragedasalternativestogoto,sincetheygenerallymakeascriptmoreunderstandableandmaintainable.Onecanalsocreatea"While"or"Do...While/Until"loopbymakingthefirstorlaststatementoftheloop'sbodyanIFstatementthatconditionallyissuesthebreakcommand,buttheuseofWhileorLoop...Untilisusuallypreferred. Thebuilt-invariableA_Indexcontainsthenumberofthecurrentloopiteration.Itcontains1thefirsttimetheloop'sbodyisexecuted.Forthesecondtime,itcontains2;andsoon.Ifaninnerloopisenclosedbyanouterloop,theinnerlooptakesprecedence.A_Indexworksinsidealltypesofloops,includingfile-loopsandregistry-loops;butA_Indexcontains0outsideofaloop. TheOneTrueBrace(OTB)stylemayoptionallybeusedwithnormalloops(butnotspecializedloopssuchasfile-patternandparsing).Forexample: Loop{ ... } Loop%RepeatCount%{ ... } Specializedloops:Loopscanbeusedtoautomaticallyretrievefiles,folders,orregistryitems(oneatatime).Seefile-loopandregistry-loopfordetails.Inaddition,file-readingloopscanoperateontheentirecontentsofafile,onelineatatime.Finally,parsingloopscanoperateontheindividualfieldscontainedinsideadelimitedstring. Related Until,While-loop,For-loop,Files-and-foldersloop,Registryloop,File-readingloop,Parsingloop,Break,Continue,Blocks Examples Createsaloopwith3iterations. Loop,3 { MsgBox,Iterationnumberis%A_Index%.;A_Indexwillbe1,2,then3 Sleep,100 } Createsaninfiniteloop,butitwillbeterminatedafterthe25thiteration. Loop { if(A_Index>25) break;Terminatetheloop if(A_Index<20) continue;Skipthebelowandstartanewiteration MsgBox,A_Index=%A_Index%;Thiswilldisplayonlythenumbers20through25 }



請為這篇文章評分?