Lost Password?


Go Back   CodeCall Programming Forum > Software Development > C and C++

C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-21-2006, 01:54 PM
Sionofdarkness Sionofdarkness is offline
Programming Expert
 
Join Date: Jul 2006
Posts: 384
Rep Power: 11
Sionofdarkness is on a distinguished road
Default 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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 07-21-2006, 04:42 PM
John's Avatar   
John John is offline
Co-Administrator
 
Join Date: Jul 2006
Age: 20
Posts: 3,478
Last Blog:
Joomla! And Incompeten...
Rep Power: 20
John has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond repute
Send a message via AIM to John Send a message via MSN to John
Default

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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 07-21-2006, 04:56 PM
Sionofdarkness Sionofdarkness is offline
Programming Expert
 
Join Date: Jul 2006
Posts: 384
Rep Power: 11
Sionofdarkness is on a distinguished road
Default

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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 07-23-2006, 05:53 PM
kromagnon kromagnon is offline
Learning Programmer
 
Join Date: Jun 2006
Posts: 53
Rep Power: 10
kromagnon is on a distinguished road
Default

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 --></
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 07-23-2006, 07:03 PM
TkTech TkTech is offline
 
Join Date: Jun 2006
Posts: 1,034
Last Blog:
Having trouble with yo...
Rep Power: 20
TkTech is on a distinguished road
Send a message via MSN to TkTech
Default

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.
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum
Chat with other CodeCall members on IRC; connect to irc.codecall.net and join #codecall
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #6 (permalink)  
Old 07-26-2006, 06:44 PM
icepack's Avatar   
icepack icepack is offline
Programmer
 
Join Date: Jul 2006
Location: North Carolina
Posts: 115
Rep Power: 9
icepack is on a distinguished road
Send a message via AIM to icepack
Default

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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 07-28-2006, 09:11 AM
smith smith is offline
Programmer
 
Join Date: Jun 2006
Posts: 108
Rep Power: 9
smith is on a distinguished road
Default

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";
}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 07-28-2006, 08:53 PM
WingedPanther's Avatar   
WingedPanther WingedPanther is offline
Super Moderator
 
Join Date: Jul 2006
Age: 35
Posts: 3,421
Last Blog:
wxWidgets is NOT code ...
Rep Power: 37
WingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to behold
Default

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 | Linux Forum
Programming is a branch of mathematics.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
MySQL Loops reachpradeep Database & Database Programming 0 03-04-2007 10:12 AM
JavaScript:Tutorial, Loops TcM Javascript 4 12-13-2006 09:53 AM
JavaScript:Tutorial, Break Out Of Loops TcM Javascript 0 12-08-2006 06:35 AM


All times are GMT -5. The time now is 11:06 AM.

Contest Stats

WingedPanther ........ 2753.6
Xav ........ 2704
Brandon W ........ 1702.32
John ........ 1207.73
marwex89 ........ 1175.24
morefood2001 ........ 966.05
dcs ........ 655.75
Steve.L ........ 475.59
orjan ........ 418.58
Aereshaa ........ 383.54

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 101%


Complete - Celebrate!

Ads