Programming in C - The For Next Loop
( Counting )
There was a time, in the not so distant past, when BASIC was the language to learn. I learned and used it along side my peers, and was actually pretty good at it. Now that C has come along, new challenges arise, along with the old ones. For instance:
There are times when you would want a computer to repeat the same operation over and over. Writing ( and using ) the same portion of code thousands of times in one program is, well, tedious at best. A more elegant method, was to create a "subroutine" and have the computer "count", and with each count number, it would go back down to that subroutine. In basic, this was accomplished with a FOR - NEXT loop. It would go something like this:
10 FOR X = 1 to 10
20 GOTO 50
30 NEXT X
40 END : REM - - - END OF PROGRAM
50 PRINT X : REM - - - The Computer Prints The Result of "X" on the Screen
Of course, this oversimplified program would simply count from one to ten, then print the result out onto the screen with each of the 10 repetitions. The onscreen result would look like this:
1
2
3
4
5
6
7
8
9
10
END
This may sound like a simple concept, but it is one of the cornerstone principles of programming.
With the advent of C, there had to be a way to duplicate this function, and so, here it is:
#include <stdlib.h>
#include <stdio.h>
int count;
void main()
{
for (count=1; count < 11; count = count + 1)
{
printf(" %d\n", count);
}
exit(0);
}
Notice that this program, while less "elegant", performs the same function ( counting from 1 to 10, and printing it on the screen ). Note the command that does the actual counting. Let's look at it and explain it a little bit:
for (count=1; count < 11; count = count + 1)
It begins with a "FOR" like in basic, but there's no "NEXT" statement. Instead it reads like this:
"count" is equal to 1.
IF count is less than 11, THEN add one to count.
But why 11 instead of 10? Simple. We use the number 11 because we are continuing to count ( and print ) until we reach the number 10. When we get to the number 11, it stops counting, and hence doesn't print. If we had used the number 10, it would only display numbers 1-9.
Notice also, that there isn't a semicolen after the for (count=1; count < 11; count = count + 1) command. That's because the command isn't quite finished. Note that after it comes an open curley brace {. In actuality, this means that everything within the curley braces { } is part of the same command. In reality, C recognises that anything within curley braces { } is included in part of the command, and since the command ends with the curley brace, it doesn't need a semicolon at all!
There is a slight shortcut to this method:
#include <stdlib.h>
#include <stdio.h>
int count;
void main()
{
for (count=1; count < 11; count++)
{
printf(" %d\n", count);
}
exit(0);
}
In this case, " count++ " means the same thing as " count=count+1 "
So, now that we can make a computer count from one to ten, how can we create something useful and practical from this? Well, let's assume that we need to know the hexidecimal equivilants of decimal numbers from 1 to 32. Let's write a program that will do this. (This will also allow us to look at a different feature of C:
#include <stdlib.h>
#include <stdio.h>
int count;
void main()
{
for (count=1; count < 33; count++)
{
printf(" %X %d\n", count, count);
}
exit(0);
}
GREAT! It counted just the way I wanted it to. In all my pride, I show it to my loving wife, who looks at it and says, "What did it do?" I explain that it counted from one to 32 in Hex and Decimal. "Let me see it again". I run the program again and she says, "I didn't see any change." The problem is that the darned computer counts so fast that she can't see it! I've got to find a way to slow it down!
In BASIC, this was easy. We used what was called a "Timing Loop". We simply made the computer count from 1 to XXX, without it really doing anything else. It went something like this:
10 FOR X = 1 to 10000
20 NEXT X
So what is the C equivilant?
for (t2=1; t2 < 10000; t2++){ }
Where the greatest number of t1, in this case 10000, will determine the speed of the count. So, if we put this into the code, we can create a SLOWER counting computer ( or at least to us it appears to count slower ).
#include <stdlib.h>
#include <stdio.h>
int t1;
int t2;
int count;
void main()
{
printf(" HEX Decimal\n");
for (count=1; count < 33; count++)
{
printf(" %X %d\n", count, count);
for (t1=1; t1 < 10000; t1++) { }; /* HERE IS THE TIMING LOOP */
}
exit(0);
}
Well, that worked - sort of, but it still ran a little fast for the human eye to catch. On MY computer, I had to add a timing loop WITHIN a timing loop. Why one might ask? Simple, because C will only allow a hex figure of FFFF or ( 65535 decimal ) for a given integer ( in this case int t1 ). I could have used a float number, but I decided to go another route, and put a loop within a loop. This gives me more control over the speed anyway. Keep in mind, the faster the processor on your computer, the faster it will count, so the bigger t1 and t2 have to be, but the max is 65535. This was the final result on MY box to obtain a 1 second pause between counting:
#include <stdlib.h>
#include <stdio.h>
int t1;
int t2;
int count;
void main()
{
printf(" HEX Decimal\n");
for (count=1; count < 33; count++)
{
printf(" %X %d\n", count, count);
for (t1=1; t1 < 10000; t1++) /* The outer loop */
{
for (t2=1; t2 < 5000; t2++){}; /* The loop within the loop */
};
}
exit(0);
}
On The Following Indicator...
(
GREEN
will indicate your current location)
|
|
1
|
2
|
3
|
4
|
5
|
6
|
7
|
|
8
|
9
|
10
|
[
ElectronicsTheory.Com
]
This Course was written by Ray Dall © All Rights Reserved.
This page and all it's content Copyright, Trademarks, Intellectual Properties
and other legal issues 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 Ray Dall.
All Rights Reserved.
And for what it's worth... this page was last updated HexDate 01-11--7D1
VISITORS: