WebArtz - The Web Design Forum
Welcome to WebArtz, Guest!

WebArtz is a nice place for discussions related to web designing and coding. We help our members to code their own website templates with HTML and CSS. We give them advice on various issues.

To know more about WebArtz Forum, visit the About Us page.

At the moment, you are viewing the forum as a guest. As a guest you can't make post and participate in discussions. You need to register and become a member of the forum. Click the register link below and become a part of this forum.

Thank You


You are not connected. Please login or register

View previous topic View next topic Go down  Message [Page 1 of 1]

1Accespted 'For' Loop in JavaScript on Thu Mar 22, 2012 1:29 pm

Mr.Joker


Mr. WebArtz
Mr. WebArtz
'For' Loop in JavaScript
Using the 'For' loop in javascript


Hey guys, I am Mr.Joker and today I will show you how to use for loop in javascript. So what for we use for loop? We use it in situations when we want to repeat something without typing same code over and over. How is that possible? Well it is Very Happy . Here is example code:

Code:
<script type="text/javascript">
var x = "Welcome to WebArtz.";
for (i=0;i<10;i++) {
document.write(x + "<br />");
}
</script>

We have variable x with value of "Welcome to WebArtz.". Then we have for loop. How it works? Inside brackets first we write a value of some integer, we call it always i (most common) and set value to zero. After we do that, we set condition. I said, if i is less then 10 preform this code. So we see i is zero so code will be preformed forever. To stop that we just add i++ which mean that i will increase 0-1-2-3-4-5-6-7... till it get 10 and there it stop. So, I made a condition that some peace of code will be preformed 10 times without writing that code 10 time. Preatty cool, right? There is another example:
Code:
<script type="text/javascript">
for (i=0;i<10;i++) {
alert("I love WebArtz!");
}
</script>
Here, everything is same just the task will be preformed 10 times and that task is alert box. If you want that alert box preform infinitive (for trolling people) just do this:
Code:
<script type="text/javascript">
for (i=0;i<10;i--) {
alert("I love WebArtz!");
}
</script>
So what i did here? Just changed form i++ to i-- . Why? Well if I say number i which is zero will decrease to -1,-2,-3,-4... that mean it will never reach number 10, and thats why this script will run forever.

Notice : These tutorials are copyrighted by WebArtz Forum. You may not publish it on anywhere without written permission from the administrators.

2Accespted Re: 'For' Loop in JavaScript on Tue Apr 03, 2012 10:47 am

ankillien


Administrator
Administrator
Tutorial Accepted

View previous topic View next topic Back to top  Message [Page 1 of 1]

Permissions in this forum:
You cannot reply to topics in this forum