Search
Left Quote    Knowledge speaks, but wisdom listens.
- Jimi Hendrix    
Right Quote
 
[login] | [Register]
 

Learning JavaScript Basics

by: bs0d
Page: 7 of 10
(View All)

Loops


Loops are an important part of JavaScript. Loops control your program to whatever limits you set them to. There is a loop which executes a group of statements a finite number of times. Then there is an infinite loop, which is a loop that will never terminate.

There are 3 types of loops used with JavaScript:

  • FOR loop
  • DO WHILE loop
  • WHILE loop


It is to your advantage to use loops when you need to execute a group of statements over and over. Again, this cuts down on coding for you, and also allows your program to execute faster. You want to build your programs short and powerful, not long and slow.

FOR LOOP:


Although you have already seen a for loop used earlier in this tutorial, I will still go ahead and describe how they are used.

for (<initial value> ; <condition = true> ; <increment value>)
{
statement1;
statement2;
and so on...;
}

Example:




So what is the control variable in the code sample above? The answer is "a" do you see?

WHILE LOOP:


We have not discussed while loops, but there is a place and need for them in JavaScript. If you were to use a while loop, this is how you would construct it:

while (<condition = true>)
{
statement1;
statement2;
and so on...;
}

And this is what one might look like with actual information plugged into it:



That is it for the while loop.

DO WHILE LOOP:


This is similar to the WHILE loop, except it's just arranged a bit differently. Here, take a look:

do
{
statement1;
statement2;
and so on...;
}
while(<condition = true>);


Like all of the rest of the loops, the statements you need executed a number of times are between the { and the }. But unlike the While loop and the for loop, the condition is underneath the statements. Just be sure you say "do" at the beginning when using this loop

Example:



Did you notice anything different? Look at the condition of the loop. At the end, we must use the semicolon. This is also the only loop where a semicolon is required at the end of the condition.

BREAK STATEMENT


What if you needed a loop, but once the loop reached a certain point you also needed it to stop within the boundaries you already set? JavaScript has the BREAK statement specifically for this purpose. So if you were caught in an infinite loop, the BREAK statement could get you out of it. Once JavaScript reads the BREAK statement, the loop is dropped.

I know, you want to see it in action! So in the code sample below, I will create an infinite loop (one that would normally never end), but then use the BREAK statement to end the loop once it has reached a point I specify...



Do you see how it works?

CONTINUE STATEMENT


Like the break statement, there is also the continue statement. If you use the continue statement, it will stop execution of the loop and return back to the beginning of the loop. This means, if you place the continue statement in the middle of your loop, any statements underneath will not be executed once the condition for continue has been met. The purpose or intent behind using it is to check for special exceptions inside the loop --and skip them without disrupting the normal flow of your loop.

Example:




What do you think occurred above once executed? When the loop is processed, it takes the value of x and adds one to it. Then it checks to see if x is = to 3, if not - it will process the "document.write" statement. So the output would be: 1,2,4,5. Skipping 3 because when it got to continue, it went back up to the beginning again.

This seems like a good place to stop and show you a bit more examples using what we learned.




1  |  2  |  3  |  4  |  5  |  6  |  7  |  8  |  9  |  10  |  
Next »


No Comments for this page.

You Must be logged in or a member to comment.


Tutorial Stats

Tutorial Stats

389,169 Views
3 Total Comments
4.66 Rating of 5 (3 Votes)

Options

Tutorial Options

· Login to Rate This Article
· Login to Post a Comment
· Read more by this author
Digg This Article! Del.icio.us: Bookmark This Article Reddit: Bookmark This Article BlinkList: Blink This Article! YahooMyWeb BlogMarks: Add This Mark! Furl: Save This Article Spurl: Mark This Article

Articles

Related    

· Getting Started with Ajax
Your Article Here



"AllSyntax.com" Copyright © 2002-2018; All rights lefted, all lefts righted.
Privacy Policy  |  Internet Rank