www.webdeveloper.com
Results 1 to 4 of 4

Thread: Beginner question on loops

Hybrid View

  1. #1
    Join Date
    Dec 2013
    Posts
    10

    Beginner question on loops

    I am just starting out with JS, and the difference between while and for loops. What is the best criteria to use when deciding which loop structure to use?

  2. #2
    Join Date
    Oct 2012
    Location
    Croatia
    Posts
    255
    For loop is mainly used when you know how many times you want to repeat your code. Contrary to that you use while loop (when you're not certain how many times you'll have to repeat your code).

  3. #3
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,335
    and in certain circumstances you use the do{}while() loop.

    The four loop ...

    This loop is for items of a length you can find, a limit you impose or general counting loops and iterating Array elements that are numbered, it does not iterate elements that have associative array names, eg x['bananas']
    Code:
    for(x=0; x<10; x++){
    // do something 10 times
    }
    This loop is mainly for iterating Object elements, these loops will iterate objects and arrays, the difference with a for..in loop is that it will iterate numbers and associative content properties where a regular for loop wont.
    Code:
    for(a in arguments){
    // do something
    }
    This loop is a conditional based loop, its dependent on a condition evaluating to true
    Code:
    while(x<totalLength){
    // loop while condition exists
    }
    it is useful to note that a while(true){} conditional loop will run forever or at least until web page close. I say useful because sometimes you won't have a conditional that fits your need and may need to "break" out when your condition finally arises, this may be a certain time and or receipt of an AJAX content. Who knows?

    This loop is also a conditional based loop and executes at least once
    Code:
    do{
    // do something once and then do a test
    }while(y!=false);
    the last loop is especially useful if one of the parameters is to be set after loop execution

    With all of the looping methods, you can always break out of the loop no matter where you are in that loop process, this can be a simple test using if(something>whatwasexpected) break;

    Loops or being able to loop or even know what is the best method to use is and important part of javascript, in fact any kind of programming language, not just javascript is important.
    Last edited by \\.\; 12-29-2013 at 05:47 AM.
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

  4. #4
    Join Date
    Jan 2014
    Posts
    1
    Your thread has helped me to understand this subject on a different level. I would like to appreciate your efforts for exploring this issue. Thank you for your information.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles