Results 1 to 4 of 4

Thread: Beginner question on loops

  1. #1
    Join Date
    Dec 2013

    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
    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
    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']
    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.
    for(a in arguments){
    // do something
    This loop is a conditional based loop, its dependent on a condition evaluating to true
    // 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
    // do something once and then do a test
    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 06:47 AM.
    --> JavaScript Frameworks like JQuery, Angular, Node <--
    ... and please remember to wrap code with forum BBCode tags:-

    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

  4. #4
    Join Date
    Jan 2014
    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