Ad space (banner)
๐ŸŽObjective-C Lessons
Lesson 5 / 68

Repeating (the for loop)

This lesson covers looping in Objective-C with a for statement, with the aim of running the same work efficiently.

A for statement repeats the same work. Alongside the C-style form, Objective-C also has fast enumeration (for...in), which loops over an array directly.

The sample code prints the numbers 1 through 5 with for (int i = 1; i <= 5; i++) — the traditional three-part form (initialise, test, update) inherited from C.

A common early stumble is choosing between the C-style for loop and fast enumeration. If all you want is to handle each element of an array, fast enumeration produces simpler, more readable code.

In professional work, this kind of loop appears everywhere you need to walk through the elements of an array in order.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Objective-C directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)