This topic is another one that took my brain a while to digest. The for-loop is an incredibly powerful and useful tool that you will find yourself needing/using all the time. It may be a little tricky to get the hang of at first but you can do it.
Let's start by recalling the situation we are in that necessitates the use of a for-loop. We need to build a big huge array and we don't want to build it by hand. To give you an idea of what I mean by hand consider this code:
Based on our exhaustive discussion that we have had on methods and method calling, can you describe what is going on here? We are calling the addObject: method on our srtrings NSMutableArray object. For each time we call that method, we are adding a string object into our array.
Based on this example, you can see how it would take a ton of work to build this array by hand. Lots of copy/paste and annoying editing. But the good news is that we don't have to do it that way! We can use a for-loop and our lives will be insanely easy.
Let's take a look at the structure of a regular (there are a few different variations) for-loop:
That's it. It doesn't look that bad, right? If you put that code into your app's viewDidLoad method, do you have any guesses what your console would look like? Let's talk about what is going on. After the for declaration, there are three separate components within the (), separated by ;'s. This is important syntax you need to get just right. We declare a local variable of type int name i that we give a value of 0. It's name and value can be anything you want. Standard convention (for whatever reason) is that you name it i, and we want 0 because that is where we start.
The purpose of a for-loop is to give you a control mechanism for-looping through some code a specified number of times. Based on our next condition i < 1000; how many times are we hoping this for-loop loops for us? from 0-999, or 1,000! Do you see how that works? This for-loop is looking awesome.
The final condition i++ does what the ++ command does everywhere else. It increments an int. So we start off our loop at 0, we make sure that i is less than 1,000, and if it is we run through the course of the loop, executing whatever code is in there, and after we run through once, we call i++ to say we have finished that round of looping.
The result is a useful tool that lets you easily control how many times to iterate over a specified bit of code.
So let's remember our goal of building this massive array of strings. Without me telling you what to do, can you think of how to put the pieces together and solve our problem? If not, that's ok. We will go over it. We want to declare the array once outside the scope of the for-loop, and then inside the for-loop we want to call the addObject: method 1000 times to build up the array. Check this out:
How cool is that?! You are going to love for-loops. They rock. Is there any part of that that isn't making sense? Can you see that because we are within the scope of the for-loop that we have access to the i integer? So at any point within the scope of the for-loop we have access to the iteration number that we are currently on. That number is called the index. Say that word 10,000 times over and eat it for breakfast. Index. Index. Index. You need to be able to use that word as interchangeable as your thumb. The index refers to the position in the array that you are currently at.
So let's tie up one last loose end before everything comes together and you will sit there in awe and wonder because of how useful arrays and for-loops are. We have just built our array of 1,000 strings, now what? How does that help us in our current project?
Remember I said the term index is very important. Whenever we hit the plus/minus button in our app, we are changing the value of a counter variable, right? This can be our index!! All we need to do is to set the label (the same code we already have inside of all of the if statements) to the value of the string that can be found in the array at index:count!
Try and figure that out. See if you can piece together the rest of this project. Are you seeing how cool this stuff is? Well, let me tell you, we are just getting started. The coolness will continue!
Ok, enough blabbing. Here is what I am talking about:
Assuming that we built our NSMutableArray as a global variable up in the viewDidLoad: method, we have access to both the array and the index variable we need, and the method objectAtIndex: goes into the array and returns whatever object is at the index position you are giving it. Pretty slick!
It is very important to know that your app will crash if you ask for an object at an index that doesn't exist. That is why I had the if statement there making sure that it wouldn't ask the array for it's 1001th object. We know it didn't have that many object so we aren't even going to ask!
Stay tuned for more goodness to come.
Thank you so much for these posts. You explain this better than I've ever seen before.
ReplyDelete