While Do/While Loops
While Loop
The while loop will test the entry condition before each iteration and only perform the next iteration if the entry condition is true.
A while loop is what's known as an entry-controlled loop, because the condition is tested prior to entering the loop body.
Do While Loop
This loop should be used when you want to execute the block of code once before testing the exit condition. The do while loop will test the exit condition at the completion of each iteration. If the exit condition is false, the loop will exit.
The do/while loop is what's known as an exit-controlled loop, because the condition is tested prior to exiting the loop body.
Exercise
Prompt the user for a number between 1 and 100, if they enter a number, then start with the number entered and calculate the sum of every number from 1 through the number entered. Use the alert function to return the sum.
For example, if 10 is entered the sum would be 1+2+3+4+5+6+7+8+9+10 = 55.
Last updated
Was this helpful?