Welcome to Dream.In.Code
Getting Help is Easy!

Join 105,772 Programmers for FREE! Ask your question and get quick answers from experts. There are 1,483 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



loops

 
Reply to this topicStart new topic

loops, define loops in easy way

sharjeel
post 19 Mar, 2008 - 02:18 AM
Post #1


New D.I.C Head

*
Joined: 19 Mar, 2008
Posts: 0

want to know about loops in easy way
User is offlineProfile CardPM

Go to the top of the page


red_4900
post 19 Mar, 2008 - 01:17 PM
Post #2


D.I.C Addict

****
Joined: 22 Feb, 2008
Posts: 502


My Contributions


how about posting this in the correct thread first?

welcome to DreamInCode by the way!! have fun here~ whatsthat.gif
User is offlineProfile CardPM

Go to the top of the page

gabehabe
post 19 Mar, 2008 - 03:41 PM
Post #3


T3H R0XX0R!

Group Icon
Joined: 6 Feb, 2008
Posts: 2,054



Thanked 40 times

Dream Kudos: 1425

Expert In: C, C++

My Contributions


Loops contain a piece of code which the program will run depending on a certain condition. For example, you decide if you decide you want a cup of tea, you get one. If you don't want one, you don't get one.
This would be expressed as:
CODE

if (I want a cup of tea)
    Get a cup of tea;
else
    Don't get a cup of tea;


NOTE: That isn't actually genuine code tongue.gif
User is online!Profile CardPM

Go to the top of the page

cdbitesky
post 21 Mar, 2008 - 05:47 PM
Post #4


New D.I.C Head

*
Joined: 20 Mar, 2008
Posts: 6


My Contributions


QUOTE(gabehabe @ 19 Mar, 2008 - 03:41 PM) *

CODE

if (I want a cup of tea)
    Get a cup of tea;
else
    Don't get a cup of tea;


not actually a loop, just an if statement.
a loop is a command that has 2 main parts.
The LCV or loop control variable which is a viable that decides where the loop is and how many times it needs to repeat.
The Sentinel value, which is the final value for the LCV to reach before terminating.
ex.
CODE

exLCV = 0
exSet = 10
while(exLCV < exSet)
{
print "blah number: " + exLCV
exLCV = exLCV + 1
}


This post has been edited by cdbitesky: 21 Mar, 2008 - 05:48 PM
User is offlineProfile CardPM

Go to the top of the page

NickDMax
post 22 Mar, 2008 - 01:49 PM
Post #5


2B||!2B

Group Icon
Joined: 18 Feb, 2007
Posts: 2,672



Thanked 26 times

Dream Kudos: 475
My Contributions


lol, its funny to see how little people know about loops. smile.gif

generally a loop is just what is sounds like. My favorite loop is this little bit of BASIC code:
CODE
10 GOTO 10


QUOTE
Calculon: Have you got an extra GOTO 10 line? I said I don't need a bender.


Of course that loop is not very useful in most programs.

A loop is a bit of code the repeats. In a FULL LOOP you will have the following:
An initialization step
An exit condition.
An "increment" step
A loop block

A classic for loop tend to be a good example since it has all the parts:

for (initialize; condition; increment) {
loop block
}


so a loop in C might look like this:
CODE
int i;
for (i = 0; i < 10; i++) {
    printf("I can count to: %d", i);
}


Now most loops have all of these parts. They can be hidden away but they are normally there.

So, in the for loop I used 'i' as a LCV (loop control variable) as cdbitesky pointed out. Most loops have this variable, it is initialized in the initialization step, it is the one that is in the condition, and the one that changes in the "increment" step.

the increment step may or may not actually "increment" the variable -- the point is, that in this step the value of the variable changes (making it possible for the exit condition to exist).

so lets take another stupid loop, lets say that we have a function (mousePress()) which returns true if a mouse button has been pressed, else it returns false:

CODE
while (!mousePress()) {
print "press a mouse button will ya!!!"
}


In this loop things are all hidden from us... the initialization step is actually just a logical idea that function mousePress() returns false while no key is pressed... the "increment" step is inside the function as well, its whatever mechanism changes the return value of the function when a mouse key is pressed. The condition is the only real visible step.

Not all loops actually have all the steps, some loops have multiple versions of some of the steps. Many loops will mix arround the order in which the steps occur (for example a while-loop vs. a do-while-loop...).
User is offlineProfile CardPM

Go to the top of the page

Tom9729
post 22 Mar, 2008 - 01:53 PM
Post #6


Debian guru

Group Icon
Joined: 30 Dec, 2007
Posts: 1,387



Thanked 7 times

Dream Kudos: 325
My Contributions


If you don't know what a loop is, you shouldn't be programming. smile.gif
User is offlineProfile CardPM

Go to the top of the page

cdbitesky
post 23 Mar, 2008 - 06:32 PM
Post #7


New D.I.C Head

*
Joined: 20 Mar, 2008
Posts: 6


My Contributions


QUOTE(Tom9729 @ 22 Mar, 2008 - 01:53 PM) *

If you don't know what a loop is, you shouldn't be programming. smile.gif

Where exactly should he start.
This forum isn't just for expert programmers.
User is offlineProfile CardPM

Go to the top of the page

red_4900
post 23 Mar, 2008 - 07:17 PM
Post #8


D.I.C Addict

****
Joined: 22 Feb, 2008
Posts: 502


My Contributions


It is one of the basic things that u should know. IMO, loop is still very easy to learn and it's not that hard to master it. if u really cant understand what loop is, then maybe programming isnt made for u. smile.gif

maybe u should go google it and try learning on ur own before asking here. only when u have something u dont understand will u come here and ask other people. because asking 'what is a loop' is just like asking others to spoon-feed u. moreover, it's too general.

This post has been edited by red_4900: 23 Mar, 2008 - 07:24 PM
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 8/21/08 03:27PM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month