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 7 of 7

Thread: While Loops

  1. #1
    New Coder
    Join Date
    Apr 2007
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    While Loops

    Ok, I got great results last time I asked a question here. I hope it works again.
    My assignment says this:
    Write while loops to emulate each of these for loops:
    Code:
       1.
    
          for (j = 4; j > 0; j --) { 
          document.writeln(j + "
          "); 
          } 
    
       2.
    
          for (k = 1; k <= 99; k = k*2) { 
          k = k/1.5; 
          } 
    
       3.
    
          for (num = 0; num <= 10; num ++) { 
          if (num == 8) 
          break;
          }
    I can do most the the work on this myself, but I have never been very good with loops...Can anyone help or offer advice?

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,593
    Thanks
    0
    Thanked 645 Times in 635 Posts
    A for loop defines three values.

    1. The value to set before starting the loop
    2. The condition to test to continue looping
    3. The value to change each time around the loop.

    Set the first of these before the while loop, the second as the loop condition, and the third goes in the end of the loop.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #3
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Pretty much it goes like this:
    Code:
    for(a;b;c){
        d
    }
    is equal to
    Code:
    a
    while(b){
        d
        c
    }
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #4
    New Coder
    Join Date
    Apr 2007
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok, for some reason I still cant get it to work. Can anyone tell me what the code should look like or what would be displayed after the loop is ran.

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,899
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Here is an example, but we cannot do your homework for you.

    var i = 0;
    var number = 50;
    while (i < 10) {
    number --;
    i ++;
    }

    alert ("Loop stopped at iteration " + i + "\nNumber is now " + number);

  • #6
    New Coder
    Join Date
    Apr 2007
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok, I understand that you cant tell me how to do homework. Thats ok. But can you tell me if it is possibly right or way off? Because this is what I have so far:
    Code:
    j = 4
    while (j > 0)
    {
    document.write(j + "<br>")
    j--
    }
    It works, but that doesn't always mean it is correct...today seems to be a day to me that proves that... If you cant say correct or not, thanks anyways for all the help you have given me.

  • #7
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,033
    Thanks
    0
    Thanked 248 Times in 245 Posts
    That's correct.


  •  

    Posting Permissions

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