Thread: Loops

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    1

    Loops

    I'm doing the beginners C++ tutorial on this site, and i was wondering what loops are actually used for in programs? I think I've figured out how to use them but i'm not sure what to use them for.

  2. #2
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    Say I wanted to add up all the numbers from 0 to 10. I could do:
    Code:
    int main(void)
    {
    	int Total = 0;
    
    	Total = Total + 1;
    	Total = Total + 2;
    	Total = Total + 3;
    	Total = Total + 4;
    	Total = Total + 5;
    	Total = Total + 6;
    	Total = Total + 7;
    	Total = Total + 8;
    	Total = Total + 9;
    	Total = Total + 10;
    	
    	printf("The total from 0 to 10 is %d", Total);
    
    	return 0;
    }
    But, maybe I'd need the sum of all the numbers up to 100000. Now that could be a problem.

    So, a loop would come in handy (either a For Loop or a While Loop) because it would take 100000 lines of code and condense it into:
    Code:
    int main(void)
    {
    	int Total = 0;
    
    	for(int Counter = 0; Counter <= 100000; Counter++)
    	{
    		Total = Total + Counter;
    	}
    	
    	printf("The total from 0 to 100000 is %d", Total);
    
    	return 0;
    }
    Or a similar While Loop.

    Now, it's not every day you'll need to add up the numbers from 0 to 100000. But say you're reading a text file line by line, instead of writing out "fscanf..." an unknown number of times. You'd use a Loop with a condition that kicks you out once you reach the end of the file.

    Loops are also good for initializing arrays of variables. For example. int MyArray[1000]. And I want everything to start off as 0. I hope you can see why a loop would be useful.

    There are tons of uses for them though, much deeper than the examples above. Pretty much any time you need to repeat code, a loop will handle the job for you nicely with a limited amount of stress.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >i'm not sure what to use them for.
    You use them for doing the same thing over and over. When you need to do something over and over, a loop might be in order.
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    197
    And it is quite common to be doing the same thing over, and over, and over. There are alot of programs that just sit in one continous loop all the time (games for one). Also, many servers (such as HTTP,FTP) will use a loop of something like (in pseudo code)
    Code:
    Listen for incoming connection
    recieve connection
    send any needed info for the client to get started
    loop starts here
         get command from client
         process command
         reply to client
         go back to begining of the loop unless client wishs to quit
    end loop
    all of that would actually be in a loop as well, if you want to server to work with more then just one client. That was greatly simplified though, but I hope it gets the idea across.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Loops Trouble
    By rlframpton in forum C Programming
    Replies: 2
    Last Post: 04-17-2009, 01:08 AM
  2. Too many loops D:
    By F5 Tornado in forum C++ Programming
    Replies: 6
    Last Post: 12-03-2007, 01:18 AM
  3. recoursion or loops?
    By Mecnels in forum C++ Programming
    Replies: 2
    Last Post: 01-14-2002, 12:09 PM
  4. help with arrays and loops
    By jdiazj1 in forum C Programming
    Replies: 4
    Last Post: 11-24-2001, 04:28 PM
  5. for loops - newbie q's
    By Narciss in forum C Programming
    Replies: 8
    Last Post: 09-26-2001, 02:44 AM