If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Explain: particular WHILE condition

I don't seem to understand the while condition and would like to know if someone has a better explanation. I understand the while loop will only execute if the condition is true. So now I ask you how can this code "run" successfully if I enter the number 11? the while loop has a condition that checks if topNumber(11) < 1 or topNumber > 100. How can code/statements below the while loop execute if 11 is not less than 1 or greater than 100 which will make this a false condition. Maybe I'm going around circles but I hope someone else has a better explanation as the javascript runs perfectly fine.

It's because this is not a "true" while loop. This is do while loop. It's used rarely in JS because in fact there's no such thing that you cannot do without it. This is the syntax of Do While loop:

Code:

do
{ //some code }
while (condition) //specifies condition

This is how while loop looks like:

Code:

while (condition)
{
//do something
}

The main difference is that the while loop executes only if prior to the code specified condition is true. On the other hand do while loop makes sure that your code inside "do while" statement gets executed at least once regardless of specified condition being true or false.

This is the reason why your code gets executed only once (in case of 11), calculates the sum and then exits.

Thank you very much it helps a lot! I think I was going around circles with this issue and now it's clear as water.

If I enter a number less than 1 or greater than 100 an alert message will appear as "top number must be greater than..." and program will exit and will not enter while loop. On the other hand if I enter a number such as 11 it will enter the while loop successfully and return the sum because the do loop must execute ONCE correct?

No, if you enter number less then 1 or greater than 100 the code inside the loop will continue executing as long as this condition is true. It will alert a message "Top number must be greater than zero and less than 100" and will keep you prompting for another input as long as you keep entering numbers less than 1 or greater than 100. On the other hand if you enter number greater then 1 and less than 100 code inside the loop will execute just once and exit. (It wont' return sum, sorry for this, didn't take a look at your code. The sum is calculated later on)

Do while loop is just like the while loop the only difference is that with the do while loop you get sure that your code will get executed at least once regardless of condition specified.

Consider this example:

Code:

var blnLoop = true; //specifies boolean for the while loop
while (blnLoop == true)
{
var n = prompt("Enter a number: ","") //enter some number to stop the loop
if (!(isNaN(n)))
blnLoop = false; //if a number was entered stops the loop
}

With the above code if you want your code inside the while loop to get executed you have to define blnLoop variable. On the other hand take a look at the following code:

Code:

do //code will execute at least once
{
var n = prompt("Enter a number: ","")
}
while (isNaN(n)) //code inside the loop statement will continue to execute as long as specified condition is true

As you can see the code do the same stuff only with the Do while loop it wasn't necessary to specify blnLoop variable for loop to get executed.

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.

Does the while loop reinforce the Do loop to have a successful execution? For example, it will continue to prompt user for a number until a number from 1-100 is entered and then it will exit.

do {
// code inside the curly braces that will execute at least once!!
}
while (condition) //specifies condition that if evaluated to TRUE will force the code inside the do statement to execute as long as the specified condition is TRUE

You can't have do statement without the while statement that comes exactly after the set of curly braces. They work in tandem. On the other hand it's completely legal to have while statement that stands "alone" (that doesn't include do statement).