do while loop in JavaScript




Link for all dot net and sql server video tutorial playlists
http://www.youtube.com/user/kudvenkat/playlists

Link for slides, code samples and text version of the video
http://csharp-video-tutorials.blogspot.com/2014/11/do-while-loop-in-javascript.html

In this video we will discuss do while loop in JavaScript with an example. In Part 16 we discussed while loop. Please watch Part 16 before proceeding.

while loop :
1. while loop checks the condition first
2. If the condition is true, statements with in the loop are executed
3. This process is repeated as long as the condition evaluates to true.

do while loop :
1. do while loop checks its condition at the end of the loop
2. This means the do…while… loop is guaranteed to execute at least one time.
3. In general, do…while… loops are used to present a menu to the user

What is the difference between while and do while in javascript
1. while loop checks the condition at the beginning, where as do…while… loop checks the condition at the end of the loop
2. do…while… loop is guaranteed to execute at least once, where as while loop is not.

do while loop example

var userChoice = “”;
do
{
var targetNumber = Number(prompt(“Please enter your target number”, “”));
var start = 0;
while (start [= targetNumber)
{
document.write(start + “[br/]”);
start = start + 2;
}

do
{
userChoice = prompt(“Do you want to continue – Yes or No”).toUpperCase();
if (userChoice != “YES” && userChoice != “NO”)
{
alert(“Invalid choice. Please say, Yes or No”);
}
} while (userChoice != “YES” && userChoice != “NO”);
} while (userChoice == “YES”);

Original source


21 responses to “do while loop in JavaScript”

  1. Hi Venkat,

    This program is not working correctly..!!!

    >>> First the prompt box is opening ,once i have entered the number and click OK ,it quickly asking "Do you want to continue -yes or No"..the numbers is not displaying in browser..

    I have attached the code below…!!

    CODE :
    ————————————————————————————————————————————————————————————————
    var ques = " ";
    do {
    var user = Number(prompt("Please enter the number", " "));
    var start = 0;

    while (start <= user) {
    document.write(start + "<br />");
    start = start + 2;
    }

    do {
    ques = prompt("Want to continue – Yes or No");
    if (ques != "Yes" && ques != "No") {
    alert("Please enter Yes or No");
    }
    } while (ques != "Yes" && ques != "No");

    } while (ques == "Yes");
    —————————————————————————————————————————————————————————————–

  2. Thanks for all your work. Helping me a lot.

    Instead of Number(prompt()), can you help me on how to use it through function with onclick event and also adding # through text box in webpage?

  3. hi Venkat.,

    here is my interview questions .
    1.How would I get alert if my credential already being used by some other user? 
          Eg: suppose I have share my user/pw to some other and he loged in with those credentials. in this case if I loged in with same credentials I should get alart at my end.

    my answer : we can have to create a flag column in database table and we do with it.

    but he said not like this (classic).

    2.How do we hide the file extension in url.
    my answer is "url redirecting" .

    he said no..

    Can u pls give you suggestion for the same.

Leave a Reply