Examples of Objective-C Loops

by mattjdrake on April 10, 2009

Using loops to repeat actions in programming is a common task. Objective-C does this chore in more or less the same way as other programming languages. However, if you are still unfamiliar with the Objective-C syntax you may feel a little disoriented.

So, to help out I wrote out some examples of for, for-each, do and while loops for you:

//Loops

//For loop
     for (int y = 0; y < 3; y++) {
     NSLog(@"y = %i", y);
}

//Do loop
x = 0;
do{
     NSLog(@"x = %i", x);
     x++;
}
while(x <= 4);

//While loop
x = 0;
while (x <= 4 ) {
     NSLog(@"x = %i", x);
     x++;
}

//For each loop
//Create an array and add elements to it
NSMutableArray *anArray = [[NSMutableArray alloc] init];
[anArray addObject:@"Element 1"];
[anArray addObject:@"Element 2"];
[anArray addObject:@"Element 3"];

//Use a for each loop to iterate through the array
for (NSString *s in anArray) {
     NSLog(s);
}
//Release the array
[anArray release];

There you go – feel free to add any of your own examples on the comments below!

Here Is How To Fast Track Your Objective-C Programming Skills…


Click here or the box to the right to access to your online workshop now…

Each lesson comes packed with comprehensive video, source code and text. When appropriate I include hands-on exercises. Check out the list below to see what is specifically covered in each lesson:

Module 1 – Getting Started With iPhone App Development

- Lesson 1 – Overview of iPhone OS
- Lesson 2 – Introduction to Tools: XCode, Interface Builder & iPhone Simulator
- Lesson 3 – Your First App
- Lesson 4 – Super-Charge XCode

Module 2 – Learn How to Program in C

- Lesson 1 – What is Programming?
- Lesson 2 – C Programming Basics and Specifics
- Lesson 3 – Functions
- Lesson 4 – Variables and Arrays
- Lesson 5 – Program Flow
- Lesson 6 – Loops
- Lesson 7 – Complex Data with Struct
- Lesson 8 – Putting It All Together

Module 3 – Master Object Oriented Programming With Objective-C

- Lesson 1 – What is Object Oriented Programming?
- Lesson 2 – Objects
- Lesson 3 – More Strings, Lists and the For Each Loop
- Lesson 4 – Memory Management
- Lesson 5 – Designing Your Own Classes
- Lesson 6 – Extending Classes With Categories
- Lesson 7 – Protocols & Key-Value Coding

Module 4 – No-BS Cocoa-Touch With iPhone SDK

- Lesson 1 – Overview of Cocoa-Touch + Model-View-Controller
- Lesson 2 – Using Interface Builder (The View)
- Lesson 3 – Target-Action and the View in Code
- Lesson 4 – Delegation
- Lesson 5 – Super-Charging Your View With Interface Builder
- Lesson 6 – Model & App Architecture

Click here or the box to the right to access to your online workshop now

Please share this if you like it!
  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • email
  • FriendFeed
  • LinkedIn
  • MySpace
  • Ping.fm
  • StumbleUpon
  • Suggest to Techmeme via Twitter
  • Technorati
  • Tumblr
  • Yahoo! Bookmarks

{ 1 trackback }

iPhoneKicks.com
April 15, 2009 at 8:26 am

Comments on this entry are closed.

Previous post:

Next post: