Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: Explain: particular WHILE condition

1. ## Explain: particular WHILE condition

I'm trying to understand why this particular while loop executes if I decide to enter 11 as the TopNumber prompt. From my understanding the while loops only execute if the conditions are TRUE. It checks to see if TopNumber is < 1 or > 100. If I enter 11 this would make the conditions false and the while loop should not execute? I'm having a difficult time understand any clear explanation would be very helpful.

PHP Code:
``` <!DOCTYPE html> <html lang="en"> <head>     <meta charset="UTF-8">         <title>Sum the Numbers</title>        <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>             <script>         alert("Sum the Numbers from 1 through entry");                  do {             var topNumber = prompt("Enter top number to sum", 5);             topNumber = parseInt(topNumber);             if (isNaN(topNumber) || topNumber < 1 || topNumber > 100) {                 alert("Top number must be greater than zero and less than 100");             }         }         while (isNaN(topNumber) || topNumber < 1 || topNumber > 100);                  var sum = 0;         for (i = 1; i <= topNumber; i++) {             sum += i;         }         alert("Sum of numbers from 1 through " + topNumber + " = " + sum);              </script> </head> <body>     <section>         <h1>This page is displayed after the JavaScript is executed</h1>     </section> </body> </html>  ```

• I accidentially double posted ... see answer below

• Yes, a while loop would work like this, but you are using a do/while

- A while loop will evaluate the condition first, then execute the code if the condition is still met
- A do/while loop will execute the code first, then evaluate the condition, then execute the code only if the condition is still met

• so the do loop *must execute once and if a number from [1,100] is not entered then the appropriate if condition will be accessed followed by the corresponding message. and the prompt message will ask again for another number between 1 and 100 correct? and then program will exit successfully if a *correct number is entered, for example 11?

I hope I'm right.

• A do/while loop tests the condition at the end of the loop and so always runs at least once. A while loop does the test at the top of the loop and so will not run at all unless the condition is satisfied.

Note that alert and prompt in live scripts became obsolete when Netscape 4 died and now that all browsers have a debugger built in there isn't even a use for them for debugging. About the only remaining use for alert is as a placeholder for the part of the code that hasn't been written yet while testing the part you have written.

• I like the prompt command, cant see the benefit of the DOM / document / .inner html method that makes it 10x more complex!

How do you activate the browsers debugger??
That would be v useful

• Originally Posted by donna1
I like the prompt command, cant see the benefit of the DOM / document / .inner html method that makes it 10x more complex!

How do you activate the browsers debugger??
That would be v useful
prompt has been obsolete since browsers were able to access values from form fields. Also some browsers assume that it is being used for debugging and display extra checkboxes. It can also be easily disabled in the browser. The same issues apply to confirm and alert. My browser is set to never display confirm or prompt boxes and to only allow alert to display when running a local script.

Using innerHTML is far simpler than creating your own custom alert (so as to ensure you are not asking your visitors if they want to turn off JavaScript).

The only time it makes sense to use alert() is for unit testing of JavaScript functions before adding them to the rest of your code. There is no circumstance where it is necessary to use confirm() or prompt() any more.

In most browsers the debugger is accessed via the F12 key. In all browsers there is an option in the browser menu that will open the debugger and which tells you the key that opens it directly.

Also with just a couple of extremely rare exceptions ALL JavaScript should be placed just before the </body> tag. You are placing your script in the wrong place and that's why using the correct calls is more difficult.

• ## Users who have thanked felgall for this post:

donna1 (11-12-2013)

•