Loops in JavaScript

22. October 2009 19:34

Loops

The main reason computers work is that they loop through code. The computer wouldn’t be practical if it only went linearly and you had to keep repeating the same code. With a loop you can use the same section of code repeatedly, until an ending condition is met.

 

The two main loop styles are “for” loops and “while” loops.

With the “for” the instructions for starting, incrementing, and the condition check are all rolled into one statement. The “for” loop is used when you want a segment of code to run a certain number of times. This is where those comparison operators come into play. The code will run as long as the condition is true.

i.e. 

var x = 0;

for(x = 0; x < 10; x++)

{

 document.write(“Hello”);

}

In the case above, the code will be executed as long as the value of variable x is less 10.

“Hello” will be written ten times on the screen.

 

The “while” loop on the other hand will infinitely run through a block of code as long as the condition is met. The start condition and increment conditions are run outside of the loop condition.

i.e.

var x = 0;

while(x<10)

{

 document.write(“Hello”);

 x++;

}

This code will also print “Hello” ten times on the screen. But you can see how it is set up differently.

Alternatively, there is another way to get out of a loop. And that is the “break” statement.

Using this in your code will cause the loop to quit and continue with the rest of the code.

i.e.

var x = 0;

for(x = 0; x < 10; x++)

{

 document.write(“Hello”);

 

  if(x == 4)

  {

     break;

   }

}

In this case, “Hello” will be written to the screen five times because the loop is broken on the fifth iteration.

Another handy statement is the “continue” statement. This causes the current loop to quit and start on the next loop iteration.

i.e.

var x = 0;

for(x = 0; x < 10; x++)

{

 document.write(“Hello”);

 

  if(x == 4)

  {

     continue;

   }

}

In this case, “Hello” will be written to the screen nine times because the loop jumps a number when x is equal to four.

 





Comments

11/26/2009 8:13:32 PM #

Ways to Lose Weight

Hey, I just thought you might wanna know that you're blog's sidebar isn't showing up properly in K-Meleon. I'm using Linux though, so I'm not sure if the problem is from me.

-Sandy

Ways to Lose Weight United States |

12/1/2009 12:03:13 AM #

How to Lose Weight

Hey, I just thought you might wanna know that you're blog's sidebar isn't showing up properly in K-Meleon. I'm using Linux though, so I'm not sure if the problem is from me.

-Melinda

How to Lose Weight United States |

12/20/2009 4:27:40 PM #

paydayloans

Nice resource. rss feed added

paydayloans United States |

12/22/2009 4:32:03 PM #

网页设计

Thank you for your article

网页设计 People's Republic of China |

2/15/2010 10:25:34 PM #

pay day loans

The men who succeed are the efficient few. They are the few who have the ambition and will power to develop themselves.

pay day loans United States |

3/28/2010 11:53:48 AM #

payday loans

When you're taking care of the customer, you can never do too much. And there is no wrong way... if it comes from the heart.

payday loans United States |

4/22/2010 8:22:22 PM #

computers keyboard

Thanks for a great blog. I was able to get the information that I had been looking for. Thanks once again!

computers keyboard United States |

4/23/2010 3:58:29 PM #

disc player

Your blog is very interesting. I would like to tell that I have been looking for such information and finally got it. Thanks a lot.

disc player United States |

6/3/2010 12:58:04 PM #

games funny

Hey blogger, thanks for sharing this post. I found it first-class. Regards,  !

games funny United States |

Comments are closed


About the author

 The creators of the Web Design Blog have been creating web designs for over 12 years

Page List