+ Reply to Thread
Results 1 to 8 of 8

Thread: Loops

  1. #1
    Programming Expert Sionofdarkness is an unknown quantity at this point
    Join Date
    Jul 2006
    Posts
    383

    Loops

    How are loops used in programs? When are they used, and what exactly do they do? I've heard of loops before, but never knew exactly what they were used for.

  2. #2
    Co-Administrator John is a glorious beacon of light John is a glorious beacon of light John is a glorious beacon of light John is a glorious beacon of light John is a glorious beacon of light John's Avatar
    Join Date
    Jul 2006
    Age
    21
    Posts
    5,882
    Blog Entries
    25
    a loop allows you to keep iterating(looping) something untill a condition is true...ive only ever used a loop in a mathamatical program i developed... if my memory serves me right there are two types of loops the while loop and the for loop.

    a basic loop would be something like this


    while (variable > 0) {

    //do this

    }

    basically it says while the variable is grater than 0 keep doing the code inside the braces

  3. #3
    Programming Expert Sionofdarkness is an unknown quantity at this point
    Join Date
    Jul 2006
    Posts
    383
    Thanks a lot man, that seems like a very useful function. I can think of dozens of uses for loops right off the top of my head.

  4. #4
    Learning Programmer kromagnon is an unknown quantity at this point
    Join Date
    Jun 2006
    Posts
    53
    This is what I love about this forum. In most forums if someone asked a question common to new programmers(such as this question) they would get flamed.
    <!-- comment comment comment --></

  5. #5
    The Crazy One TkTech will become famous soon enough TkTech's Avatar
    Join Date
    Jun 2006
    Location
    Canada
    Age
    18
    Posts
    1,549
    Blog Entries
    1
    sadly your right kromagnon, it usually does happen. Most of the members of this forum are beginners themselves, and they work together to learn something, which is always nice to see.

  6. #6
    Programmer icepack is on a distinguished road icepack's Avatar
    Join Date
    Jul 2006
    Location
    North Carolina
    Posts
    115
    It's important to know about the different kinds of loops and when to use each one. For instance, if you know you have to run something a certain amount of times(lets say 10 times), you'd use a for loop
    Code:
    for(int i=1; i <= 10; i++){
    this action will be done 10 times, no more or no less(unless somehwere in the code the variable i is overridden and set to 10);
    }
    or let's say you are taking input from a user and they are to enter 0 when they are done(these loops can be evaluated by T/F). the do-while loop
    Code:
    do{
    cin >> length;
    code actions
    } while(length != 0);
    the while loop is similar to the do-while loop, except for the fact taht regardless of what happens, the do-while loops body will be executed at least once.

  7. #7
    Programmer smith is an unknown quantity at this point
    Join Date
    Jun 2006
    Posts
    153
    Perfect icepack (rep given). The main thing here is how you want to execute your loop. Like icepack said, if you need it to happen at least once use the do-while. In most cases you will see either the for or just the while.
    Code:
    for (int i;;) {
       cout << "Smith";
    }

  8. #8
    Super Moderator WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther's Avatar
    Join Date
    Jul 2006
    Age
    36
    Posts
    11,609
    Blog Entries
    57
    Also, for-loops and do-while loops can always be done using a while loop. The choice to use one of the others is usually an indication of what your intention is.
    CodeCall Blog | CodeCall Wiki | Shareware
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. MySQL Loops
    By reachpradeep in forum Database & Database Programming
    Replies: 0
    Last Post: 03-04-2007, 10:12 AM
  2. JavaScript:Tutorial, Loops
    By TcM in forum Javascript
    Replies: 4
    Last Post: 12-13-2006, 09:53 AM
  3. JavaScript:Tutorial, Break Out Of Loops
    By TcM in forum Javascript
    Replies: 0
    Last Post: 12-08-2006, 06:35 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts