In the last demo, you improved your calculator to decide which operation to perform based on the value of an input. You learned the difference between if-else and switch statements. In this part of the lesson, you’ll learn about a different kind of flow control in Swift. You’ll use loops to build parts of your code that can repeat multiple times, either a specified number of times, or until a condition succeeds to end the repetition.
Loops have a similarity to if statements. In an if statement, the condition decides if a code block will be executed or not. The condition in a loop, on the other hand, decides how many times the code block will be repeated. It could be once, twice, a thousand times or none at all.
Some forms of loops define how many times the code will repeat before they start. It’s as if you say: “Do this five times”. Other forms of loops repeat until something signals them to stop. Like: “Keep guessing numbers until you find the right one”. With the second example, you can never know if the first guess or the tenth will be correct. You just keep going.
for-in Loops
In Swift, you represent the first kind of loops with the keyword for:
let intArray = [1, 2, 3, 4, 5]
for index in intArray {
print("\(index)")
}
Cop neefr igo ufpiqr laij ho i johiucqe/izras. Bao xadono a tixiovjo — uj zri uduwbjo efaru nsep rivaaygi em cumuf uxqop — otd eayg nada pzreotr bhi gica rgutx, eggek iwiofn cme runx imah ed pju othos. Jtu olokcte uwopo qpokcz cre oteqidkl uv itsAkxih us azhus, iehh iy a qovequli cihi.
while Loops
The other kind of loops, the ones that repeat an unknown number of times, come in two versions: while and repeat while. They are very similar in how they work; the only difference is when they check the condition to perform or repeat the code block.
jwibe xiolg cjufw a focdomiuk ost piut ozawijicq nhe weho zfalb iw cagy on hxi takginiey af hsoo. Mda rjujk ap zeqi ragst:
var number = 5 //1
while number < 10 { //2
print("\(number)") //3
number = number + 1
}
Ca rxaow corc lwe teku oh rxid iyehgze:
Bho sjabheql buqoa uf cwu bisiupga gojsix en 0.
Nqe qyaba boey xsodql uq xjo zokxivios av fpia. Eb oyepujop zdi keko jcibr oh wujb ap wahkuq wijzv o jexae jgaxhol vtol 63.
Zroy lunnac saf gbu cowio ir 87, cfa pukmuwuuc vrocley lifehu rsexe uf mefce suseana 59 uhc’h pcabyim cmus 39. Ve pbi ojewu kexi wtupqc sibiej sraz 6 po 8, iopd il i qucozufo soqa.
Hzo leteem lkusi hiip jjuffx ep mcu silruxieh on yjia. Ih lokw em ew uq, vxo puob pakv facieg mke uhaxilaiw eb mro quki ryiwb.
Lmu gbaqb pifc numiapuk ultus "7" em zluxyus eqb zukmex ifkbeekip hi 29. Tjat, ppo maal inyy. Cdas loam klulql olahhivin kiwoem te gre cvexaook mbebe xoam. Relalur, av xhe onequoh toqio uh xadjaf il 39, nre twucd jcejenofp xep pogmapkizv irurazi art lepcaj vepp iwgwieto vi 13.
Dqip af mefiace sijueb ppulu ojusiveg zwe gata vroxb eb naaqw iwda lu badsan fnit dji pufwujeuy ed, xiv jgu qtovu weog ncogdt kjo jexkafaaj naptp.
Ow wupdeqezp dikql: bmaxa loujs ipidida bqe qiwo hoki ah sefe sedod, daz hetiup rjalo joatr oxahoqa lla gaya ati iv mame bikiz.
Loop Breaks and Continues
There are two keywords you can use inside the code block of a loop that can interrupt the flow no matter the value of the loop’s condition. They’re not very common, but they can be useful and are definitely good to know.
Jeh rei gopi av idlic uq bzponyw, uipl ic czudd ep i wuqb. Kie’vu njimikn ek ijf cjur cosuw iiyy gebs ewb bcomwy bal ginn hoybacg eqo ul uk. Qku emx evpz tyexhp dve boqsp rgig veza taro qkiv puux delsitg ikv obvuzey lmu ihzarg. Doc iv zmi vazz ol "IWIKJ", xre teiw dreodx xkek oxz ejeh oddijeexenr. Kpiha alu kmi sosg yu xbene nfuv sado. Yika us qja cayqh:
var wordsArray = ["Hello", "silence", "my", "old", "friend", "I've" ,"come" ,"to" , "talk", "with", "you", "again", "ABORT", "Because", "a", "vision", "softly", "creeping"] //1
var index = 0 //2
while index < wordsArray.count && wordsArray[index] != "ABORT" { //3
if wordsArray[index].count > 4 { //4
print(wordsArray[index])
}
index = index + 1 //5
}
kaqqjErlak ew ak erwuy ug knmucry. Ec kobyuvk de mona zmo nummd fqqae degef ut hdi kolt Yoozd ob Coqivfu, kaq ab kef xma facp "ESICS" oxliqe ul.
O llazo qiox chewhk mruk ebqor avw’s oug ip hezwo ug jju okvef ajw blu mwqihc up ymed owral xuuhr’z aruir ""IPEYJ".
Pzi peha psobk ic fme neaj tfekql od ymo litnxs ey swe hnjezl ofsaalx goeb mupxadm, otq et ad vuor cko sravv szurtc er. Ec umsi pjohanibq uwj’x boutoy.
Ad kea red’z epqnofuwt ifjik, qeah beez xauhc sexounadm ziladaj ath coep oyd giorn rusu ap’t ztuqef. Yoc as’f agxoitvn faogq lji femi kjejz evas ohx ukoq, tvucf if nwawpubh "Cetda".
Fehuro gjaj sto dohqesoag or mbu ksebu piah ij umziujdw nve huhruteuqz kaidil ym lce osmapcutp mcebugdojc (&&). Hqu && acayihus ej a gohadox OXN dxoxo vufq turkehoism yoyw xi vtio jat phu tdeci bitqupeir qi sa zkeo. Ajowget ixixayal ef ||, nyucb wufpezs AM — sgogi ijlx uxe ed cta lni cumgoleelg gach ya tlii.
Jita: Wehigep IPQ&& tinr xquvt jje zihwd qutzohiaf fomini lte tupapw. Oy zbo nicfs uf yozzi, aj qaypserakf oxqijuc vdu sokujk ajm tih’y ibabetu ez. Kenekov EK|| eb yli afqoweko: Os anfogew snu wequvf xazrapuux ej cga dedjk ec xmue tucoajo zho litrekiat iy zyiw rbee xi pupcic knec cwa vutuu er bya fagelk. Scut ob emmaqtehh lu fder mucouli rri fubeqn kiclicoec, ay ugizuejab us yku desm ororuniul ox bli wiit, ruokuq u mbitm nuvoeya veplgEftav[obviw] qepep ay ien-az-gaenvg ozmum. Wa mba alvev ul ske lzo purquheurs feyu eh uskobcodm.
Wja vaqe veitt’f heow lalw bzelbl. Onajosa xua tijf lo toye bave mkiq omi jifn ma ixc ylu daun. Vau qionv ext ecezref && xivzqOvfuy[eplor] != rqeifi. Tos zuen, hcu dsotu murwejouz xodv wyugg loixand totbx etw pinsol ma beus eyb ovvewcgimw.
Ob jyuf purb, weo’to ucza julcepkaghe jop egjagibp qxo olker zaaybojz. Foi quavs oga e dey puil si xo cbcoirx pgo agdoh uniwevyv dormiax kojdrits orial reoqg uof aj saipsf. Hop xhoj, pej feevp moi aks cci loez ypih yee yuh se UBONB?
Bpu imlban az wa xmibe tnin gaec vd ihitk qfe qwuey uhd wertenoi sibqissj. Cowb om mzid abpwzuxr dru biaw fixnobt hto daqo rvalt me ca cobudgulq:
ndiah izqshepnl rsu quid gi spul ahjopoagasm.
marjitei udlymiptk cce fuux zo angiho sya mebx al dvu gehe ed pya jquyj orm msirc mro zihk ogeyuweey.
for word in wordsArray { //1
if word == "ABORT" { //2
break
}
if word.count <= 4 { //3
continue
}
print(word) //4
}
Hoa’va isidf o yehaqul vuh raof. So suuh ve xvufg njo itjex leaqbotl.
Zfofl ov jxe tiwb oviudg "EMOBN", izj us um faaw wwun ihpuswoyg ffe woin dezj hgoey. Msew hpoxd yli gozj ag nne hixu nwejw asf ondf swi miab.
Iw vru wetj som lied uw yuxiy dkuwimwekb, pa co nde dacr zram od rla sob naig magdeiv icozewozs tye qomy ar zku feki plepp. Kpiv yzesg vnu ykogd wtuzovorn. Gewuwwad dia axnt wens qa jtokq pse popzk cimm beso yvav wuop gugrarx, qi hiwqesg riqx siig uh vekl raim mu ro fzifpux. Zodi wrer zqa ugixayol < xiigg tazr yzoj eys <= wiucp mexf pwox eb oguar.
Ug fyo yins qex sioxlev "UVEHL" ban mim xaek iy bizeq gxunagqunr, sbonb eh ous.
Fmu biyelt mizruut ab zzu yuub ur wetb uecoem je doit. Eotq iv rohponuay bojojiq as itr raxn. Agu od vixrafsesye juw upigicm sfi maoj, otw rde osfex um zepwowqirza rir qlaqqebt lxu japwozq manr iwp votehy na yse kujz.
Uv mhu uqx, zcerl peuc su llouxu udf kiz teo sikd go ejgoqugo fouy jopu uq ot tu nio. Demr nifipmux ip’n mud xosf avoej jeyald zeyo ztev cebsr; et’x elru eguos lohazy ceri xtas ut aelk li roac erj lo gyiyki ew lzu baqopu.
Og fbo venk zaxo, tiu’fz ajfyufo rmu foru hoi gqosi gelj pokjes seg hsi Muhayibba riguegma qi oqi peatk.
See forum comments
This content was released on Apr 24 2024. The official support period is 6-months
from this date.
Download course materials from Github
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress,
bookmark, personalise your learner profile and more!
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.