Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8

Thread: Help with loops

  1. #1
    New to the CF scene
    Join Date
    May 2007
    Location
    http://www.webdevforumz.com/
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Help with loops

    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++){

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,912
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    See http://www.w3schools.com/js/js_looping.asp

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

    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

  • #3
    New to the CF scene
    Join Date
    May 2007
    Location
    http://www.webdevforumz.com/
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #4
    New Coder
    Join Date
    Dec 2006
    Posts
    80
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Hi,

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

    Code:
    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:

    Code:
    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.

    Joe
    Last edited by LFCFan; 06-04-2007 at 12:00 PM.
    http://www.Kanotix.com --My OS of choice, the best hardware detection on the planet!-
    http://distrowatch.com -Linux is the future-
    http://www.packetstormsecurity.org -Security Gurus-

  • #5
    New to the CF scene
    Join Date
    May 2007
    Location
    http://www.webdevforumz.com/
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,912
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    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.

  • #7
    New Coder
    Join Date
    Dec 2006
    Posts
    80
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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.

    Cheers
    Joe
    http://www.Kanotix.com --My OS of choice, the best hardware detection on the planet!-
    http://distrowatch.com -Linux is the future-
    http://www.packetstormsecurity.org -Security Gurus-

  • #8
    New to the CF scene
    Join Date
    May 2007
    Location
    http://www.webdevforumz.com/
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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


  •  

    Posting Permissions

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