Perl do while - Perl Tutorial

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

The last statement exits the do...while loop immediately. It acts as the break statement in C/C++. ... In this tutorial, you've learned how to use the Perl do... SkiptocontentHome»PerldowhileSummary:inthistutorial,you’lllearnaboutPerldowhileloopstatementthatexecutesacodeblockrepeatedlyaslongasaconditionistrue.IntroductiontoPerldo…whilestatementThePerldo...whileloopstatementexecutesacodeblockrepeatedlyaslongasatestconditionistrue.Bothwhileanddo...whilestatementsterminatetheloopifthetestconditionisfalse.Unlikethewhilestatementthatcheckstheconditionatthebeginningofeachiteration,thedo...whilestatementcheckstheconditionattheendofeachiteration.Thefollowingillustratesthesyntaxofthedo...whileloopstatement:do{ #codeblock }while(condition);Codelanguage:Perl(perl)Becausedo...whileloopstatementcheckstheconditionattheendofeachiteration,thecodeblockinsidetheloopalwaysexecutesatleastonce.Also,doisnotaloopblock.Therefore,youneedtouseotherstatementstocontroltheloopincludingnext,lastandredostatements.Thefollowingflowchartillustratesthedo...whilestatement:Perldo…whileloopexampleInpractice,youusethedo...whileloopstatementwhenyouwanttheloopexecutesatleastonebeforetheconditionischecked.Atypicalexampleofthisisthecommand-lineprogramthatrequestsusersforinputuntilanexitcommandisprovided.Forexample:#!/usr/bin/perl usewarnings; usestrict; my$command; print("Enteracommand,byetoquit.\n"); do{ print(">"); #convertcommandtolowercase chomp($command=); $command=lc($command); #displaythecommand print("$command\n"); }while($commandne"bye");Codelanguage:PHP(php)Whenyouenterthe by stringinthecommandline,theconditioninthedowhilestatementbecomesfalsethatterminatestheloop.Thefollowingistheoutputoftheprogram:Enteracommand,byetoquit. >perl perl >perldowhile perldowhile >perldountil perldountil >bye byeCodelanguage:JavaScript(javascript)Perldo…whilewithnextandlaststatementsThefollowingsectionshowsyouhowtousethenextandlaststatementsinsideado...whilestatementtocontroltheloop.1)Perldo…whilewithnextstatementThenextstatementallowsyoutostartthenextiterationoftheloopandskipstherestofthecodebelowit.Tousethenextstatementinsidethedo...whilestatement,youhavetodefineanadditionalloopblockforthenextstatementasfollows:do{ #doblock { statementnext; } }while(condition);Codelanguage:Perl(perl)orinshort:do{{ statementnext; }}while(condition);Codelanguage:Perl(perl)Seethefollowingexample:#!/usr/bin/perl usewarnings; usestrict; my@a=(1,3,2,4,6,9,8); my$sum_even=0; my$num=0; do{{ #getthenextarrayelement $num=shift(@a); #skipiftheelementisoddnumber nextif$num%2==1; #calculatetotalofevennumbers $sum_even+=$num; }}until(!scalar@a>0); print("$sum_even\n");Codelanguage:Perl(perl)Howitworks.Wehaveanarrayofintegersthatcontainsbothoddandevennumbers.Insidethedowhileloop,weremovethearrayelementbyusingtheshift()function.Westartanewiterationiftheelementisanoddnumber,otherwise,weaddituptothe$sum_evenvariable.Theloopterminatesonlywhenthearray@aisemptyspecifiedinthecondition.Thefollowingistheoutputoftheprogram:202)Perldo…whilewiththelaststatementThelaststatementexitsthedo...whileloopimmediately.ItactsasthebreakstatementinC/C++.Tousedo...whilestatementwiththelaststatement,youneedtoaddanotherblocktothedo...whilestatementlikethis:loop_label:{ do{ lastifexpression; }while(condition) }Codelanguage:Perl(perl)Seethefollowingexample:#!/usr/bin/perl usewarnings; usestrict; my@haystack=qw(132459867); my$count=scalar@haystack; my$i=0; my$needle; print("Enteranumbertosearch(1-9):"); $needle=int(); find_needle_in_haystack:{ do{ if($haystack[$i]==$needle){ print("Number$needlefoundatposition$i\n"); #exittheloop last; } #nextelement $i++; }until($i==$count); }Codelanguage:Perl(perl)Thefollowingistheoutputoftheprogramwhenyouenter5:Enteranumbertosearch(1-9):5 Number5foundatposition4Codelanguage:JavaScript(javascript)Inthistutorial,you’velearnedhowtousethePerldo...whilestatementtoexecuteacodeblockrepeatedlybasedonaconditioncheckedattheendofeachiteration.Wasthistutorialhelpful?YesNoPreviouslyPerlwhileLoopUpNextPerluntilStatementSearchfor:GettingStartedBasicPerlTutorialPerlI/O



請為這篇文章評分?