A step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.
A C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.
This tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.
Creating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.
Introducing loopsTeaches you the loops in JavaScript. Each loop has its prototype and a simple example showing you how to use it. |
On Tuesday, April 6th 2004 at 07:11 PM By Andrew Pociu (View Profile) ![]() ![]() ![]() ![]() (Rated 4 with 11 votes) |
||
|
Contextual Ads
More JavaScript Resources
Advertisement
LoopsYou will not believe how important loops are in programming. Loops help a program decide what to do. From the simplest thing that may sound like “While the visitor visits the website between 7AM and 10AM say ’Good morning’” to “If browser is Internet Explorer use x function, if browser is Firefox use y function”. Now, an example more suitable for JavaScript will be choosing between two different types of scripts, one for Internet Explorer browser and one for Netscape, because of some incompatibility between the two. No matter where, loops are frequently used in programming. There are several types of loops and you will learn them all by reading the below lessons.
if ... else ifWhat if you want to check if ‘x’ is 0, 1 or 2?
forWith this loop, a piece of code can loop as many times as counted by the loop. Below is the prototype for the ‘for’ loop.
whileThe “while” loop is somehow similar to the “for” loop. Let’s see how the prototype looks…
do... whileThe ‘do… while’ loop is identical to the ‘while’ loop with one small exception: it executes at least once. That’s because the condition is verified after the script looped once.
Our previous script, with the variable ‘x’ set to false from the beginning, will still execute once, using a “do… while” loop because the condition is verified after the first loop. Therefore, the following script will display “1” in the browser. var i = 0; var x = false; do { i++; document.write(" " + i); if (i == 20) { x = false; } } while(x); There are many more to say about loops and lots of examples to give. Go google |
|||
Digg It!
Del.icio.us
Reddit
StumbleIt
Newsvine
Furl
BlinkList
|
|||
|
|||
Current Commentsin if conditions;
must be like this "if (x==0)" not "if (x=0)"
Sorry, that was one hell of a common, simple mistake to do.
please give me a typical use of a "do-while" loop....... and its significant difference with the "while" loop.
The significant difference is, as I stated in the tutorial, that no matter what the condition returns (true or false), it executes at least once, because it checks the condition after.
This loop isn't used very often. Usually do...while loops are used to perform array operations.
One example of typical use of the do...while loop can be found here:
<a href="http://academ.hvcc.edu/~kantopet/javascript/index.php?page=js+iteratives&parent=js+statements&printme=true">http://academ.hvcc.edu/~kantopet/javascript/index.php?page=js+iteratives&parent=js+statements&printme=true</a>
Where it says:
A more practical use for this loop might be to check whether something has a value. For instance, you could walk an array as long as you keep finding values.
var x = 0;
do {
if (aName[x]) {
bName[x] = someProc(aName[x]);
}
x++;
}
while (aName[x]);
In the preceeding example, we take advantage of the fact that non-existent value return false. This example assumes all the array elements are contiguous, since it will stop executing with the first empty array element it finds.
try to simplify examples for while nd do while loops. thnkx
Although these are simple yet not for the bigners
dear friend,I'd like to bring it to ur kind notice, that,
if, if-else, and if-else if
are all not loops, but control structures.
There is no iteration, or continued re-execution of same set of codes here.
for, while, and do-while are loops, coz there's iteration.
kindly correct.
Decision making and branching statements are:
if, if-else, if-else if
Decision making and looping statements are:
for, while,
plz tell the main difference b/w these loops and also tell on which conditions a particular loop is applied
Related Tutorials
Related Source Code
JavaScript Job Search