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 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Aug 2006
    Location
    Upstate New York
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cool Loops and break statements.

    I need to write a loop that adds numbers from users...but need to offer a chance to get out of the program with a break statement. Any ideas?

  • #2
    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
    You said it: break

    for(........){
    if(condition){break}
    }

    But you may also use a while loop (in your case):
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <meta http-equiv="Content-Style-Type" content="text/css">
    <meta http-equiv="Content-Script-Type" content="text/javascript">
    <script type="text/javascript">
    onload = function(){
    var conf=true;
    var num='';
    while(conf){
    num=num+prompt('Enter a number','')+' | '
    var conf=confirm('Continue!')
    }
    document.getElementById('mydiv').innerHTML=num
    }
    </script>
    </head>
    <body>
    <div id="mydiv"></div>
    </body>
    </html>
    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
    •