We already
saw the use of break in switch case. Similarly, break is used in while,
do-while and for loops to come out of a loop.
break.html
<!DOCTYPE html> <html> <head> <title>while loop</title> </head> <body> <script type="text/javascript"> var number = parseInt(prompt("Enter a number less than 100", "10")); document.write("<h2>Even numbers from 0 to " + number + " are </h2>"); var i = 0; while (i <= number) { if (number >= 100) { document.write("Input should be < 100"); break; } document.write(i + "<br />"); i += 2; } </script> </body> </html>
Load above
page in browser, it prompt for input, if you enter a number which is >=100,
it prints the message "Input should be < 100" and come put of the
loop, else it print all the even numbers from 0 to given number.
No comments:
Post a Comment