Friday, 24 August 2018

JavaScript: do-while loop

do-while
do-while is also a loop construct like while, but evaluates its expression at the bottom of the loop instead of the top.

Syntax

do {
   //statement(s)
} while (expression);

Above statement executes block of statement until the condition is true.

doWhile.html
<!DOCTYPE html>

<html>

<head>
    <title>do-while loop</title>
</head>

<body>
    <script type="text/javascript">
        var number = parseInt(prompt("Enter a number", "10"));

        document.write("<h2>Even numbers from 0 to " + number + " are </h2>");

        var i = 0;

        do {
            document.write(i + "<br />");
            i += 2;
        } while (i <= number);
    </script>
</body>

</html>

Load above page in browser, it prompts for a number, and print all the even numbers from 0 to the number given by user.



Previous                                                 Next                                                 Home

No comments:

Post a Comment