.
carbonize.co.ukcarbonize.co.uk

Y!Tunnel
Trillian
YahELite
Opera
Ad Aware
Ultraedit
YahSEek

Advertisements:


Send honey to your loved ones

[Get Opera!]

Synfire's Guide to C Programming
5. Program Control

        Here I will show you how to make your program repeat parts of code as many times as needed.
This section will use comments to explain what is going on, I don't like going in too much on loops because they are fairly simple and it shouldn't take much to add them to your code.
These programs will just print 10 'x's on the screen.

do{...}while() loops

dowhile.c
-------------------------
#include <stdio.h>

int main()
{
	/* Count is created and set to 0 */
	int Count = 0

	do /* The loop starts */
	{
	Count++; /* Count is incremented (increased by 1) */
	printf("x"); /* prints an x on the screen */
	} while (Count < 9) /* tests to see if it needs to 
				  start over again. */
	return 0;
}
-------------------------

while(){...} loops

while.c
-------------------------
#include <stdio.h>

int main()
{
	int Count = 0; /* Count is created and set to 0 */
	while (Count < 10) /* Checks to see if Count is less than 10 */
	{		      /* if it is then the program repeats */
	printf("x");
	Count++; /* this increases the value of Count by one
		    but 'Count--' would decrease the value by one
		    another thing is that if the '++' or '--' is
		    before the variable the variable is changed
		    before the program goes to the next line */
	}
	return 0;
}
-------------------------
for(;;;){...} loops

for.c
-------------------------
#include <stdio.h>

int main()
{
	int Count; /* This time the Count variable is not given a
		      value */
	for (Count = 0; Count < 10; Count++)
	{ /* Count is set to 0; Count is tested; Count is incremented */
	printf("x");
	}
	return 0;
}
-------------------------
        That should be clear enough for you to learn from.
Loops aren't all that hard, but are extremely important! Never under estimate the power of a well written loop!

;) lol . o O ( Sorry, I always wanted to say that! )


Back to top | Contact me