The do...while Loop
The do...while loop is just a variant of the while loop. In such loop the block of code is always executed atleast once. The loop will repeat as long as the specified condition is true.
Syntax:
do
{
// Enter here the code to be executed
}
while (var<=endvalue); E.g. Using do…while loop to display numbers
Output: Number:0 Number:1 Number:2 Number:3 Number:4 Note: This loop will always be executed at least once, even if the condition is false, because the code is executed before the condition is tested. 4. JavaScript Break and Continue JavaScript has two special statements that can be used inside loops: a. break: The break command will break the loop and continue executing the code that follows after the loop (if any). E.g Using break statement to come out of the for loop
Output:
The number is 0
The number is 1
b. Continue: The continue command is used in case you need to break the current loop and continue with the next one.
E.g.
Output:
Number:0
Number:1
Number:3
Number:4
Number:5
4.4.3 JavaScript Popup Boxes
In JavaScript you have the ability to pickup the user input or display small amount of text to the user using dialog boxes. There are kinds of popup boxes: Alert box, Confirm box, and Prompt box. These dialog boxes appear as separate windows and their content depend on the information that has been input by the user.
Note: The content of these dialog boxes is independent of the text in the HTML page and do not effect content of webpage in any way.
The three types of dialog boxes are:
1. Alert Box: You can use alert box if you want to make sure information comes through to the user. It is the simplest way to output small amount of textual information to browser’s window. The javaScript alert() method takes a string argument and displays an alert dialog box in the browser window when it is invoked by appropriate code in javaScript. The alert box will display string passed to the alert() method and the ‘OK’ button. User cannot proceed without clicking on the alert button.
Syntax:
alert("text message ");
E.g. Using Alert Box to message user
Output:
2. Prompt Box: You can use prompt box to input a value before entering a page. When a prompt box pops up, the user needs to click either "OK" or "Cancel" to proceed after entering an input value. In case user clicks "OK" button prompt box returns the input value where as if the user clicks "Cancel" the box will return null value.
Syntax:
prompt("text message ","defaultvalue");
E.g. Prompting user to answer a question using Prompt Box
In case user answers correct i.e. user enters 25. Following screen will be displayed on the browser:
3. Confirm Box: JavaScript provides a confirm box to prompt user for verification or confirmation before continuing further with the execution of JavaScript. When a confirm box pops up, the user will have to options either to click on "OK" or "Cancel" button to proceed. In case user clicks "OK", the box returns true and if the user clicks "Cancel", the box returns false.
Syntax:
confirm("text message");
Example:
As the browser finds confirm box following output will be generated.
Comments