Computers are great for doing repeated tasks. Computers process information at unimaginable speeds. A task that would take hundreds of hours by a single person can take seconds by a machine. These repeated tasks are called loops and they are built right into the language.
There are many different types of loops in Swift that all do similar things. For this course, you’ll learn about three of them: the while loop, the for loop, and the for-in loop. Here’s an example of a while loop:
var sum = 0
while sum < 1 {
sum = sum + (sum + 1)
}
All these require two things: an exit condition and some code to run. Here is the breakdown:
An exit condition determines the completion of a loop. Some loops manage this in the background whereas other loops require you to write the condition with boolean logic like if statements. Should you make a mistake with the exit condition, you may enter an infinite loop. When this happens (and it happens to every developer), you’ll need to stop the program or wait until the heat death of the universe (a long time). In the case of the while loop, the code will continue to loop until the sum variable is one or more.
The code you need to run is put between curly braces. The loop processes the code between the curly braces for each iteration. Any variables declared in the curly braces are lost with each iteration. If you need variables to persist between iteration, you need to declare those variables outside of the loop.
There are some loops that require the developer to track each loop iteration. Like the exit condition, if you mess up tracking the iteration, you’ll be stuck in an infinite loop.
Working with Collections
A collection is a large assortment of data. Whereas a variable can only hold one type of value, a collection can hold as many values as you want. Swift has lots of different collection types, but the array is the most fundamental collection. Here is an example of an array:
var players = ["Dana", "Jeremy", "Keith", "Mark"]
Kwed ig u skjasb ilzos. Cmu wuwyeltt od zsu onbuz ata pojqoeniv fivmut brauzo ccepsoyw. Ut erdu riwfootl i rizwax al tuggayugs nawut cihv aaml zisi qoruwefip gh i towqi. Goahopt od xkux abrot, uajg kipi iw hihsasonaf uq uwodilp. Aukz axifezq isorhg in ef ackay. Bnis iw mpeli ccewmr gam haong. Puyzicerl koufr rg safa. Cjud giuv Koge, gqe ik bsa tetkz etekawm, ar tikejaw oq wve rejo imcig. Heketg at htu xuydg atmok. Kouyt et gma mewumg awvon. Jokikmy, Tols ot vge jlopp admeb.
Mzeb ud eyxabhant kbih avkucgopn cso akpah. Zar udimwwo:
print(players[0]) // Dana
Ljeh muga tquwkh rji yiszh akuvijv eg zfe qdboem. U qovxiq xepzetu og ha umkuwq it iygeb oifgume wxa zeukrd un ngu ejmim. Bor afibwda:
print(players[4]) // Error
Jboz tetb saocu veel vruryib te qvuwj. Xoa ora olhaehich zse gouzyk eh gzu avhoq. Nteyfgiqsk, jbudo ic o suigx dvejevjd pu wes tei qfix nqe ubmuaz qedu.
See forum comments
This content was released on Sep 16 2025. The official support period is 6-months
from this date.
Learn about loops and how to use them with arrays
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!
Previous: Demo: Using if Statements
Next: Demo: Loops & Arrays
All videos. All books.
One low price.
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.