JAVASCRIPT

 Home  Computers & Internet  Web Programming JAVASCRIPT
What is Javascript?
Javascript Placement
Syntax
Reserved Words
Variables
Data Types
Escaping Characters
Concatenation
Arithmetic Operators
Assignment Operators
Comparison Operators
Boolean Operators
Conditional Operator
If Statements
Else If Statements
If Else Statements
Switch Statements
While Loops
For Loops
Do While Loops
Break Statement
Continue Statement
prompt()
alert()
Date()
Event Handlers
String Object Methods
Math Object Methods
Window Object Methods

DO WHILE LOOPS IN JAVASCRIPT

A do while loop in javascript executes a block of code between the curly braces at least once before a condition is checked - this is because the condition is placed after the code to execute. Subsequently, if the condition remains true, the block of code will keep executing. Execution will stop as soon as the condition evaluates to false.

The general format for the do while loop is:

do
{
code to execute;
}
while(certain condition)

Example:

i=5;

do
{
document.write(i+"<br>");
i++;
}
while(i < 10)

We start by declaring a start value of i=5. Since do while loops execute at least once before the condition is checked, document.write(i+"<br>"); displays the number 5 (i=5 initially). Then i is incremented by 1 (i++) to equal 6. This i value of 6 is then checked against the condition while(i < 10) to test if it is less than 10. If it is (it is in our case), then the code between the curly braces execute and document.write(i+"<br>"); displays 6.

The process is repeated until i attains a value of less than 10 ie. finally i must be 9. When this happens, the do while loop stops. Therefore, in our above example, 5, 6, 7, 8 and 9 are outputted.

Home | Privacy Policy | Terms Of Use | Contact Us