Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Loops

loop

  • Please log in to reply
7 replies to this topic

#1 Sionofdarkness

Sionofdarkness

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 370 posts

Posted 21 July 2006 - 09:54 AM

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.
  • 0

#2 John

John

    CC Mentor

  • Moderator
  • 4450 posts
  • Location:New York, NY

Posted 21 July 2006 - 12:42 PM

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
  • 0

#3 Sionofdarkness

Sionofdarkness

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 370 posts

Posted 21 July 2006 - 12:56 PM

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.
  • 0

#4 kromagnon

kromagnon

    CC Regular

  • Just Joined
  • PipPipPip
  • 47 posts

Posted 23 July 2006 - 01:53 PM

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.
  • 0

#5 TkTech

TkTech

    The Crazy One

  • Retired Mod
  • PipPipPipPipPipPipPip
  • 1144 posts
  • Location:Ottawa, Ontario

Posted 23 July 2006 - 03:03 PM

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.
  • 0
Helpful CODECALL Links: Join Us, Guidelines, FAQ, Post a Tutorial

#6 icepack

icepack

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 106 posts

Posted 26 July 2006 - 02:44 PM

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
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
do{
cin >> length;
[I]code actions[/I]
} 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.
  • 1

#7 smith

smith

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 149 posts

Posted 28 July 2006 - 05:11 AM

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.
  • 0

for (int i;;) {

   cout << "Smith";

}


#8 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 28 July 2006 - 04:53 PM

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.
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/






Also tagged with one or more of these keywords: loop

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download