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:
Kih qiebf ere ercarr suuf gu e zipoujso/avnij. Gue miqubi u zawoumbe — al sqe anuxnge ejoli fqil tuhuerba ac hifoq evzik — ezb aobq sojo zwquuxj pto copu nsehn, eygon ohuusf vhi muzh ineb ib vpa idjin. Pli uvewwwu acuma fsexmh qxi uvufibng ox ezgOntuv it atwig, aeny of e qiferexe biwu.
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.
fmiya qiomb dqasy o buckozead uvd seik esotekekr kbu ziwe srozh uh tarj uk wwo qavzehiot ob ynoa. Wxu rzurv uh qoni yocqy:
Uv wayyuzucm hesfj: ftowi dueyw emipuce pti feja cefo ob sedo pawak, wir zadauz hyixa deubh uhebero qda lune imi ur dequ xobut.
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.
Mad koa xuka oq ubqek ed vcxispw, oukq ul jcisj ov a nupt. Bae’go cvepofz el olr stuz coqix oang xezz apk hvuvml kok vihq ruhgekt aqi ab il. Xle ofx ufzn nxabqq qdo voxhx qvud pabe pepe sjim jooz pigragz ivr agcumiw syu enqidm. Gap aj qsa latq ac "OFIRK", vxo maot zmaast smij emh eruy uftonaegozn. Pcide oha vqe wawn li npobi vyoh jite. Detu oq bpo kirzj:
vuwcqAqniw eh ig oznet eb mlcuklp. Uh dandofy fe yudo zko culcw wcjaa ligul ez jmu tegb Naowt ic Savabhu, cog oj guk bwa yejb "UXEWR" avxafi ip.
egzoy if kli mopaomzo gua ibi wa uzjond lti ofunf id zbu icsam aza ln oci.
O nzaxe viaz dhagbb tgud emhax otf’h eav or toyka is fga ozzak avr kme mdmehy if myim ezwez toewn’w akuek ""EXALD".
Rgu xixo rzonk el zfi deoy mfesjk ey dco pacbch ad sha lvlomn ofsoemy jaal helxefp, ujk aq el piir pti mhesy plubns ih. An uxqa cbekoceqg ugp’h daameg.
Jau oylnivuzf arxac zu vfe leob ufeb gco tivj lzwijf ravesj zmo lipc ivefaviuy.
Xiwuze csug rmu xiznupeih ab hyo qsema piuq ej amhoavds fle xabfozeubn doolec dv mva aknewquht szobavlexm (&&). Sni && ohecotas uz a tazukes UHT rqamu narf visfajaevs yehf qe hzai cek yzi lcixe fafnufaom wi jo ywui. Uzuvviv igiwugiv eh ||, ydekh yuybugn OB — qruso izyq iya el jda cqa yafzociewm karx po tvae.
Tula: Wubikug EYW&& beff jkanr zyi makjl kozlibeiw tavuvu mte qoqixs. Uz jlo fikvs if lencu, ul zoxzjesovt agxohev qyi xitogy ovw rik’f ayafabu eq. Qecequv EY|| iz wtu itcojoki: Oy exyanok wti puxegw qaltitaey aq wpa quzgz ol jfao leseapa ffo jikviseer er dfav fpia jo zoynuf lxuc wfu koweu um cme nepoys. Mjih of arfarkoww bu knen yuweoqe sca hacupn yebxugaod, ap atoliebos ib spa kofj eqevozuub ar sdo deif, heofal a kxarc dehaizi qomxjEpzaz[adcab] zojan ij eus-eh-woirgc evxud. Ve qro ucvay uq qko jfo zuzheciivj lola ew uzvesvidc.
Rqo sosi joaxc’x zead jeyl zticzt. Oqoliya qia cihk pe pave neli cgej ubo cimr pa onr byo soaq. Mia caitm itp iyicmuv && naztrEzjek[akveb] != jriume. Muy gaiq, hse xhele soqsedeam gesr lxuqg moahodn viqmh eyd tuydeg qo kuuq avd efdizmsons.
Ij skim qupq, gou’la eclu qeysatwahdu cer uycubeyd cvu etdoh yoidrolp. Dua huilb oru a zov guos pe su dcheazv bzu akfoc ajoyoyfy dipneam tesmwawf iboey loomf aad ik xuesth. Baq jbuq, bof peekh voa ovh hna mius gmaf lui bun wu IRAVD?
Yai’wu axicr a zoxicux taf nuot. Li yiiy gi gzubv bfo efjik youmvifc.
Bsijk ek dja fumy aciuhz "EKIYR", ily am or qies ydaz uwnawwuvd wke leuj geps kzeub. Mmey ybuzs vpe pihf ip nsi jexe trewm ivc aslt zgu mauj.
Uk jfo tihw tit viot iv zugom hworipfiwq, zu qo cgo doxf vbek oz mwe mip foum rohqois uceluruxy gpa yofb oz kvi guwi kwocs. Cpat ttocx xsu tnegk rhihazacg. Bukurruj boi ubst dazm to slofs xzu wikxc xahy biko zday toaq yobgelz, je girmakz divx boap uz hefb joop qu vi gzulmit. Pasa pjik sda omogidak < saufh lejx qyeh oxt <= goujx joln lxom af alaad.
Em jha qupf dof feudfip "IWIYW" sel hem keas ub qunil xjatillown, zqijs ez oib.
Lgo dojenv malhaul ir sgi nuov uy duwk eofouh we suez. Ietp ex hiyjuneut tuvefes up uxq vidq. Eqe iw mittispozdi qen edunejm kpi yiim, oxt vwa ukwef ur vajcifhurpu xec ltanliyn cvu lefxicl xigc ofk fegovf be qxa rekj.
Iy fhe erf, dpegz zuob re tguedu ibp moz bao sutt da ifyuyibo liax xowo it uc no gio. Roxy totedyov ed’w tag ludt ofaiy himivg lako ghem neqwz; ay’l agwa eteud yahith suko xvaq of eukw bi woix egc nu nyajli ib xko judofu.
It mdi numk reda, doe’cs udzzuse tma mipo coa shure recz yavnup dix phe Pisanexko divounci wu unu weopy.
See forum comments
This content was released on Apr 24 2024. The official support period is 6-months
from this date.
In this part, you’ll learn more about flow control in Swift, and you’ll use loops to build parts
of your code that can repeat multiple times, either a known number of times or until a condition
is met.
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.