do-while, while, for and for-in loops in Javascript

By: Syed Fazal Emailed: 1642 times Printed: 2111 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

Iterative statements, also called loop statements, specify certain commands to be executed repeatedly until some condition is met. The loops are often used to iterate the values of an array (hence the name) or to work though repetitious mathematical tasks. JavaScript provides four types of iterative statements to aid in the process.

do-while

The do-while statement is a post-test loop, meaning that the evaluation of the escape condition is only done after the code inside the loop has been executed. This means that the body of the loop is always executed at least once before the expression is evaluated. Syntax:

do {

   statement

} while ( expression );

For example:

var i = 0;

do {

 i += 2;

} while (i < 10);

It’s considered best coding practice to always use block statements, even if only one line of code is to be executed. Doing so can avoid confusion about what should be executed for each condition.

while

The while statement is a pretest loop. This means the evaluation of the escape condition is done before the code inside the loop has been executed. Because of this, it is possible that the body of the loop is never executed. Syntax:

while( expression ) statement

For example:

var i = 0;

while (i < 10) {

 i += 2;

}

for

The for statement is also a pretest loop with the added capabilities of variable initialization before entering the loop and defining postloop code to be entered. Syntax:

for ( initialization ; expression ; post-loop-expression ) statement

For example:

for (var i=0; i < iCount; i++){

 alert(i);

}

This code defines a variable i that begins with the value 0 . The for loop is entered only if the conditional expression ( i < iCount ) evaluates to true , making it possible that the body of the code might not be executed. If the body is executed, the postloop expression is also executed, iterating the variable i .

for-in

The for-in statement is a strict iterative statement. It is used to enumerate the properties of an object.

Syntax:

for ( property in expression ) statement

For example:

for (sProp in window) {

 alert(sProp);

}

Here, the for-in statement is used to display all the properties of the BOM window object. The method propertyIsEnumerable() , discussed earlier, is included in JavaScript specifically to indicate whether or not a property can be accessed using the for-in statement.


Javascript Home | All Javascript Tutorials | Latest Javascript Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(0)


Be the first one to add a comment

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Syed Fazal
switch in Javascript
break and continue in Javascript
do-while, while, for and for-in loops in Javascript
The if statement in Javascript
Using && in Javascript
Using ! and !! in Javascript
Bitwise NOT, AND, OR, XOR operators in Javascript
String Concatenation using Java

More Tutorials in Javascript
Use WinSCP to transfer log files remotely using Javascript
Verifying user input in JavaScript
Pause execution in JavaScript
Javascript to display client date and time on webpage
Getting Browser's height and width using Javascript
Highlighting text on a page using CSS
Using revealTrans to do page transitions in Javascript
Password protect a web page using Javascript
Diabling Right Click option in a browser using Javascript
Scrolling message on the status bar using Javascript
window.frames[i] in Javascript
Form validation using Javascript
Using && in Javascript
The if statement in Javascript
do-while, while, for and for-in loops in Javascript

More Latest News
Most Viewed Articles (in Javascript )
Typecasting in Javascript
Using parseInt() and parseFloat() in JavaScript to convert data types to Numbers
Show how many characters remaining in a html text box using javascript
List of Keywords and reverved words in Javascript
Scrolling message on the status bar using Javascript
Getting Browser's height and width using Javascript
Javascript Basics
do-while, while, for and for-in loops in Javascript
call() and apply() methods in Javascript
Pause execution in JavaScript
History and evolution of Javascript
Using ! and !! in Javascript
The if statement in Javascript
Bitwise NOT, AND, OR, XOR operators in Javascript
Using && in Javascript
Most Emailed Articles (in Javascript)
Click to remove an element from a select html form using javascript
Bitwise NOT, AND, OR, XOR operators in Javascript
this keyword sample in Javascript
Diabling Right Click option in a browser using Javascript
Getting Browser's height and width using Javascript
Show how many characters remaining in a html text box using javascript
History and evolution of Javascript
Using toString() in JavaScript to convert data types to String
Using parseInt() and parseFloat() in JavaScript to convert data types to Numbers
Using ! and !! in Javascript
Using && in Javascript
Highlighting text on a page using CSS
Using revealTrans to do page transitions in Javascript
Javascript to display client date and time on webpage
Verifying user input in JavaScript