For in Objective-C
Objective-C provides several ways to create loops. Here are some basic types of loops.
To run the program, compile it using a command like:
Objective-C uses while
, do-while
, and for
loops. The for
loop in Objective-C is similar to the C-style for
loop in Go. While Objective-C doesn’t have a direct equivalent to Go’s range
for integers, we can achieve similar functionality using standard for
loops.
The break
and continue
statements work similarly in Objective-C as they do in other C-derived languages, allowing you to exit a loop early or skip to the next iteration, respectively.
Note that Objective-C uses NSLog()
for console output instead of fmt.Println()
. Also, Objective-C requires the use of @autoreleasepool
for memory management in command-line programs.
We’ll see other forms of iteration later when we look at collections and other data structures in Objective-C.