Getting Familiar with Objective-C / Loops pt. 2
Visitors to VTC.com will be able to view all introductory videos for each training course.
Free Trial Members will gain access to first three chapters for each training course.
Full Access Members have full access to VTC.com’s entire library of video tutorials.
Learn More
Subtitles of the Movie
The last example is the do while example. And the examples so far have checked the condition before executing the Loop for the first time. The do while Loop will perform the Loop first before checking to see if it should continue looping. This is a good choice when you want the code to execute at least once, no matter what the conditions. So to set up a do while Loop we'll type in do, and Xcode's kind enough to set up our construct for us, our statement we're just going to do the Log statement again and the same as with the while Loop we're going to have to increment our variable inside of our Loop, so I'll say plus plus i and then our condition we'll do the same thing. If i is less than or equal to the count. So as we can see here, even if the condition was false this code will get executed at least once. It'll execute the code, it's going to increment i and then it's going to check and see if i is less than or equal to count. If that test is true it'll go back up and run the code again, if it's not it just exits right on out. But it's a great way to make sure that your code is executed at least once. We'll run this in the Console. Once again we get the exact same results, just like we expected. And there's one last Loop that I want to show you. It's another for Loop, but this for Loop is for iterating over objects, so we can create an Array and I'll call this one carCompanies and I'll just create an arrayWithObjects and I'll give it some car company names and terminate it with a nil. And the way we'll Loop through this for Construct is to say for and now each one of the items that's in the Array we know this is a string so we can use NSString, so we can say for NSString car in carCompanies. Now if we didn't know what the car was, if we didn't know that was NSString, then we could also use ID and we know that in Objective-C ID just represents any object - it's a generic object. It's dynamically tied at the time of runtime. And we'll just use a Log statement here again and since we're looping through objects we're going to use that percent at symbol as our specifier. Let's go ahead and run this and there we go. It goes through each one of the objects inside the NSArray, it takes that value and puts it into the car variable and then we print that car using NSLog and we could easily use that in any other statement that we needed to, but that's how you get access to each one of those objects. This for Loop is very fast and you'll see it used a lot. So now you have four different Loop Constructs in your arsenal of coding practices. The more you use them the more you'll get comfortable in knowing which one to use in which context.
Tutorial Information
| Course: | Objective-C and Xcode Essential Training |
| Author: | Craig Williams |
| SKU: | 34088 |
| ISBN: | 1-936334-00-3 |
| Release Date: | 2010-02-22 |
| Duration: | 6 hrs / 68 lessons |
| Work Files: |
Yes |
| Captions: | Available on CD and Online University |
| Compatibility: |
Vista/XP/2000, OS X, Linux QuickTime 7, Flash 8 |
VTC Sign up & Benefits
- Unlimited Access
- 98,729 Video Tutorials (23,265 free)
- Video Available as Flash or QuickTime
- Over 1026 Courses
- $30 for One Month Access
- Multi-User Discounts Available
United States 