SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    Non-Member
    Join Date
    Jan 2008
    Location
    N43 44.4824', W079 13.9408
    Posts
    2,220
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Loop explination ?

    Code JavaScript:
    while (count<12) {
    document.write("special<br/>");
    count++;

    Could someone explain to me how the count++ works? I'm reading on this and I don't quite grasp it. Otherwise the loop will go on forever.

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,716
    Mentioned
    103 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by SiberianHuskey View Post
    Code JavaScript:
    while (count<12) {
    document.write("special<br/>");
    count++;

    Could someone explain to me how the count++ works? I'm reading on this and I don't quite grasp it. Otherwise the loop will go on forever.
    Does this updated version of that code help you to understand what's happening?

    Code javascript:
    var count = 0;
    while (count < 12) {
        document.write("special<br/>");
        count = count + 1;
    }

    The indentation becomes important, as it helps to aid your understanding of the code.

    The ++ operator that you're asking about is a shortcut that is brought over from the c language.

    The following three code snippets all demonstrate the same process of adding 1 to a value.
    They all have different pros and cons that relate to them, with the second example being my personal favourite one to use.

    This snippet uses a normal arithmetic operator to perform an addition.

    Code javascript:
    count = count + 1;

    This one uses an assignment operator called +=
    Code javascript:
    count += 1;

    And here is the increment operator ++ in use.
    Code javascript:
    count++;
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    Non-Member
    Join Date
    Jan 2008
    Location
    N43 44.4824', W079 13.9408
    Posts
    2,220
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why do you need to add a increment of 1 to the while command ? Shouldn't the variable just stop when it reaches just under 12?

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,716
    Mentioned
    103 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by SiberianHuskey View Post
    Why do you need to add a increment of 1 to the while command ? Shouldn't the variable just stop when it reaches just under 12?
    That's due to the condition is (count < 12) which doesn't automatically increment.

    I suspect that it's a learning assignment where you're expected to learn how to use a while loop.

    The count for the while loop would need to be incremented either from within the loop, or from within the condition itself.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    Non-Member
    Join Date
    Jan 2008
    Location
    N43 44.4824', W079 13.9408
    Posts
    2,220
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You have to increment to me it seems like incrementing is telling the loop to start at the beginning, unless that is what it is doing ?

  6. #6
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,716
    Mentioned
    103 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by SiberianHuskey View Post
    to me it seems like incrementing is telling the loop to start at the beginning, unless that is what it is doing ?
    You're right, in that the while loop has to keep on returning to where it starts.

    The loop keeps on happening while the condition is true, which in this case is that the variable called count contains a number that is less than 12.

    Code javascript:
    while (count<12) {
        document.write("special<br/>");
        count++;
    }

    Here is how it is interpreted:

    1. Is the count less than 12? It's undefined, so yes.
    2. document.write(...);
    3. Increase count by 1
    4. End of while loop, recheck the condition.
    5. Is the count less than 12? It's 1, so yes.
    6. document.write(...);
    7. Increase count by 1
    8. End of while loop, recheck the condition.
    9. Is the count less than 12? It's 2, so yes.
    10. document.write(...);
    11. Increase count by 1
    12. End of while loop, recheck the condition.
    13. Is the count less than 12? It's 3, so yes.

    14-42 not shown for brevity.

    43. Increase count by 1
    44. End of while loop, recheck the condition.
    45. Is the count less than 12? It's 11, so yes.
    46. document.write(...);
    47. Increase count by 1
    48. End of while loop, recheck the condition.
    49. Is the count less than 12? It's 12, so no.

    The while loop now ends and execution occurs after the while loop
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  7. #7
    Non-Member
    Join Date
    Jan 2008
    Location
    N43 44.4824', W079 13.9408
    Posts
    2,220
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The loop keeps happening because the increment keeps adding 1 and until the loop is out of range will the loop stop, and I suppose that is the whole point of loops ?

    Great help so far from you, I appreciate.


Bookmarks

Posting Permissions

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