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 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Sep 2010
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Changing For loop to While loop

    Hi,

    I am doing some studying and we was to create a small loop using either the for loop, while loop or do while loop. I chose to do the for loop because it was easier to understand, but I want to know how to do the same using the while loop. Here is the for loop I have, but I cant figure out how to change to while loop.

    Code:
    for (var i = 0; i < 5; ++i)
    {
        for (var j = i; j < 5; ++j)
    {
             document.write(j + "");
      }
      document.write("<br />");
    }
    output is equal to:
    01234
    1234
    234
    34
    4

    How do you make the same using a while loop?
    Last edited by zairne86; 11-12-2010 at 09:57 PM. Reason: Solved

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,700
    Thanks
    80
    Thanked 4,658 Times in 4,620 Posts
    LOL! Took you a whole 2 minutes to solve it?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Quote Originally Posted by zairne86 View Post
    I want to know how to do the same using the while loop
    Quote Originally Posted by zairne86 View Post
    Last edited by zairne86; 11-12-2010 at 10:57 PM.. Reason: Solved
    An unwritten Forum's rule, all over, says that you should also post the solution, even if you found it by yourself. As you had this problem today, another one might have it tomorrow, and it will be nice if you could help him in advance, wouldn't it?
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  

    Posting Permissions

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