C Loops - Expanding Your First Program
Welcome to Colour Me Cocoa.
Looks like you're new here. If you like what you see, you may want to subscribe to my RSS feed, or if you prefer sign up to receive email updates.
Thanks for visiting!
Now that you have had a chance to play with Xcode and build your first program it’s time to take a look at one of the fundamental concepts of programming. Loops. So fire up Xcode and open up the MyFirst project, if you’ve been good and played around with the simple Hello World program you might have noticed yourself repeating the same lines of code over and over again.
If you decided to print the “Hello, World!” string 5 times in a row you probably came up with something like this:
printf("Hello, World!\n");
printf("Hello, World!\n");
printf("Hello, World!\n");
printf("Hello, World!\n");
printf("Hello, World!\n");
It gets the job done, but as one of my university lecturers once put it
Your code is like a caveman who comes home to his wife and says “cook chicken, cook chicken, cook chicken”, because he does not know how to say “Cook 3 chickens”.
Fortunately C, and hence Objective-C, supports the notion of loops. Loops are a section of code that repeat for a set number of times. So lets get cracking and turn your primitive cave man code into something more modern!
for (INITIALIZATION; CONDITION; ITERATION) {
BODY
}
This is the basic syntax of the for loop. The for loop is one of the looping methods available in C, allowing you to repeat the BODY section of the loop multiples times. For our example above the BODY should be replaced with the printf statement. If your for loop only has one line in the BODY section then you can omit the curly braces, I codefer to leave them in as I find it makes the code easier to read.
for (INITIALIZATION; CONDITION; ITERATION) {
printf("Hello, World!\n");
}
In order to do something multiple times, we need some way to keep track of how many times we have already done it. Because of this we need to declare a variable, I’m going to use int i to keep track of how many times I have printed the “Hello, World!” string.
int i;
for (INITIALIZATION; CONDITION; ITERATION) {
printf("Hello, World!\n");
}
The INITIALIZATION part of the for loop is where you set your counter variable to its starting value. Typically in programming you would start with 0. So lets set that up in our for loop.
int i;
for (i = 0; CONDITION; ITERATION) {
printf("Hello, World!\n");
}
The next section to fill in is the CONDITION section. This is where you define when the loop will finish. If this condition is met, then the loop stop repeating itself. Since we want to print the “Hello, World!” string 5 times we need to make sure that i does not become greater than 4. Why 4? Because the first iteration of the loop uses 0, then 1, 2, 3, 4. We can write this as i < 5 which reads “i less than 5″.
int i;
for (i = 0; i < 5; ITERATION) {
printf("Hello, World!\n");
}
Right now our for loop will continue to print the “Hello, World!” string so long as the value of i is less than 5. This is good, but how do we increment the value of i? The ITERATION section is where we tell the loop what to do with i every time it runs. We should replace ITERATION with i = i + 1 or i++ for short.
int i;
for (i = 0; i < 5; i++) {
printf("Hello, World!\n");
}
If you’ve done everything right you should be able to build and run MyFirst and have the “Hello, World!” string printed to the scream 5 times. Excellent! You may be thinking that this hasn’t saved much over simply writing the printf statement out 5 times in a row, and you’re right. But once you start building more complicated programs these loops allow you to do iterate through thousands of different items in very little code.
There is another type of loop. It’s called the while loop. It’s basic syntax is as follows:
INITIALIZATION
while (CONDITION) {
BODY
ITERATION
}
It follows the same rules of replacing each section as the for loop example. Because the ITERATION section is within the curly braces along with the body there is no way to omit them, even if you wanted to.
int i = 0;
while (i < 5) {
printf("Hello, World!\n");
i++;
}
Finally there is an adjusted version of the while loop. It is called the do-while loop. It’s basically the same except phrased in reverse. Instead of saying while this condition is true do this, it says do this until this condition is no longer true.
int i = 0;
do {
printf("Hello, World!\n");
i++;
} while (i < 5);
You now hold the power to print 1000 (or more, or less) lines to the command line by writing a mere 4 lines of code. That’s very powerful and great for reducing the amount of typing you need to perform as a programmer. Have a play with these loops and try a few different things:
- Change the iterator, i = i + 10 ? Can you count backwards?
- Change the initialization to a different number, start higher? start negative?
- Change the condition, greater than? less than? equals (=) to? a combination of both?
February 8th, 2007 at 3:01 pm
Good entry into loops. I have done visual basic in the past but no C programing. But this is very similar. Good job.
February 9th, 2007 at 9:21 pm
Thanks Alex. The concepts are the same as the ones in most programming languages, it’s really only the syntax that changes so if you’ve programmed in another language then everything here should be familiar.
April 1st, 2007 at 2:39 am
Sir;
I am, quite intersting to learn programming pls send the concepts of c and c++ to my mail thank u