C:Loops
(Page 1 of 4 )
We left off discussing operators and how to use them in our last article. Here, we will cover those loops I've been threatening to cover for so long. You will learn of their many and powerful uses. Behold! The mighty loop...
Loop Dee Loop
Loops in C are used to repeat a certain block of code a given amount of times. This is what computing is really all about. It saves the programmer time and coding. There are several loops, or iterators as they are also referred to in C, and we will discuss them below, starting with the For Loop.
For Loop
For loops repeat a given number of times, so long as the criteria is true. The amount of times the loop is executed is decided in advance. For loops look like this: For(i=0;<10, i++). They consist of three sections. The i=0 portion initializes a counter variable (in this instance, "i"). The <10 tells the program to continue looping while i<10. And finally the i++ is known as an incrementor, and adds one to the counter variable every time the loop is repeated. Here is an example program that counts up to ten and then prints some text:
#include <stdio.h>
int main()
{
int count;
printf("nYou have until the count of ten...");
for(count=0; count <10; count++)
printf("%dn",count);
printf("Time is up!");
return(0);
}
The result of this program is:
You have until the count of ten...
0
1
2
3
4
5
6
7
8
9
Time is up!
We could of course always create a program that counts down using what is called a decrementor:
#include <stdio.h>
int main()
{
int count;
printf("nLift-off in t-minus 10 seconds...n");
for(count=10; count>0; count--)
printf("T-minus %dn",count);
printf("Lift off!");
return(0);
}
This program results in:
Lift-off in t-minus 10 seconds...
T-minus 10
T-minus 9
T-minus 8
T-minus 7
T-minus 6
T-minus 5
T-minus 4
T-minus 3
T-minus 2
T-minus 1
Lift Off!
You will note that we could have accomplished the same thing using this code:
#include <stdio.h>
int main()
{
int count;
printf("nLift-off in t-minus 10 seconds...n");
for(count=10; count>0; count=count-1)
printf("T-minus %dn",count);
printf("Lift off!");
return(0);
}
And we aren't simply limited to incrementing/decrementing by 1 either. Behold this mighty example!
#include <stdio.h>
int main()
{
int count;
for(count=0; count<9; count=count+2)
printf("%d ",count);
printf("Who do we appreciate?!?");
return(0);
}
In the above code, we increment by 2 each time through the loop, resulting in:
0 2 4 6 8 Who do we appreciate?!?
You can use multiplication as well. In this sample we increment by *52 and count to a million:
#include <stdio.h>
int main()
{
int count;
for(count=2; count<1000000; count=count*52)
printf("%d ",count);
printf("You counted to a million!");
return(0);
}
Note that the initial value of count is set to 2. Had we set it to zero, we would have created an infinite loop. This is because if we set the initial value to 0, 0 * 52 equals 0, and so the value never would have equaled 1,000,000.
Okay, one last thing before we finish our discussion of for loops. We don't always have to print the number out. Let us suppose we just want to print some text ten times. Here is how we would do so:
#include <stdio.h>
int main()
{
int count;
for(count=0; count<10; count++)
printf("James Payne is awesome and my hero! In him, I do obey!n
");
return(0);
}
This results in:
James Payne is awesome and my hero! In him, I do obey!
James Payne is awesome and my hero! In him, I do obey!
James Payne is awesome and my hero! In him, I do obey!
James Payne is awesome and my hero! In him, I do obey!
James Payne is awesome and my hero! In him, I do obey!
James Payne is awesome and my hero! In him, I do obey!
James Payne is awesome and my hero! In him, I do obey!
James Payne is awesome and my hero! In him, I do obey!
James Payne is awesome and my hero! In him, I do obey!
James Payne is awesome and my hero! In him, I do obey!
Next: While You Were Away >>
More BrainDump Articles
More By James Payne