View Full Version : Help with loops

06-04-2007, 12:15 PM
I'm aware of the different types of loops, there is a for, while and if, but can someone explain all these and how to use them in mega detail because im finding it very difficult to learn and understand them. I will be using the 'for' loop most.

Below is an example 'for' loop i found, one thing I do not understand is where they get the bits in bold from. Could someone please explain the different types of for loops, I'm really getting stressed out over them.

for(i=0; i<td.length; i++){

Philip M
06-04-2007, 12:45 PM
See http://www.w3schools.com/js/js_looping.asp

for (i=0; i<td.length; i++) {

set the starting value of the loop counter (i) to 0
continue looping for as long as i is smaller than the value of the length of the variable td
increment i by 1 each time round the loop

06-04-2007, 12:48 PM
i dont understand what you mean about increment. And where do they get the i from, I have seen some people use h, t, b.

06-04-2007, 12:55 PM

You should really try Google with this kind of question. But here's a brief explanation of your For loop:

for(i=0; i<td.length; i++){

The i=0 part is just assigning the right hand side value of zero to the the left hand variable named i. It doesn't have to be i, it could be myVar, var1, var_1 etc.

The next part: i<td.length. What we're saying here is while the i variable is less than the value of td.length; do the next part of this statement:

i++ - Increment the value of i by 1.

So what we're saying in whole:

i is assigned the value of zero (i=0; ), and while its value is less than the length of td (i < td.length; ), increase i by 1 (i++) before starting the loop again. Once i is the same length as td, the loop will stop executing.


06-04-2007, 12:58 PM
I have tryed googling, but i find it very difficult to understand the for tutorials.

i know your all going to get annoyed with me but what do you mean by increase i once before starting the loop, and you know you said i=0? could you set it to i=20% or something like that

im so sorry :(

Philip M
06-04-2007, 01:05 PM
Sorry to be unkind, but if you cannot understand such a simple and basic programming concept as this (or the meaning of the word 'increment') after it has been explained to you twice over then you really ought to consider an alternative activity such as underwater motor cycling.

06-04-2007, 01:06 PM
Thats ok, we all have to start somewhere.

I don't think you could set it to 20% because you'd need to know what it was 20% of. You could assign a variable with the value of 20% to the Right hand side, (i = myVar; ) but I find it hard to imagine a sequence that would include 20% in it, therefore you'd end up with an eternal loop.

And I didnt say before starting the loop, I said before starting the loop again.


06-04-2007, 01:10 PM
i did not really want to say this but as you posted such a salcastic post, the reason i have trouble is because i have learning difficulties. if you would rather have a go at me could you please at least do it by PM instead of in public eye.

EDIT: thank you for understanding LFCFan :)