SMF For Free Support Forum
Signup For Free Forum
November 21, 2008, 11:18:09 pm *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: Welcome to SMF For Free. The best free SMF Host
 
   Home   Help Search Arcade Gallery Login Register  

Pages: [1]
  Print  
Author Topic: A better understanding of for loops  (Read 378 times)
0 Members and 1 Guest are viewing this topic.
slayer766
SMF For Free Member
*
Offline Offline

Posts: 36



View Profile WWW
« on: February 04, 2008, 07:30:12 pm »

For loops basically loop through something you specify and will execute forever until you either input a break, an if condition, or simply tell the loop to end at a certain number. Here are some examples of the three I said:

Example 1: Breaking a for loop with break.

Breaks can be used in functions, loops, etc. I will show you how to use the break in a for loop.
Code:
<script type="text/javascript">
for(i = 0; i < 5; i++){
document.write(i);
break;
}
</script>

That will basically stop the loop at 0, because it breaks the code after it writes 0. Breaks are quite effective to haulting something.


Example 2: Stopping a for loop with an if condition.

An if condition will just basically stop the loop once it reaches X amount.
Code:
<script type="text/javascript">
for(i = 0; i < 5; i++){
if(i == 3){
document.write(i);
}
}
</script>

That will count all the way up to 3 and then write the number 3 and go no further than that.


Example 3: Completeing a for loop with a specified number
Code:
<script type="text/javascript">
for(i = 0; i < 5; i++){
document.write(i);
}
</script>
It's as simple as that. The loop will count up to 5, which it will become 4 because the loop started on 0.
Logged

Check our Code Index which currently contains 453 codes!



Pages: [1]
  Print  
 
Jump to:  

cheap low cost web hosting reviews

Powered by SMF 1.1.7 | SMF © 2006-2008, Simple Machines LLC
ServerBeach Coupon
Page created in 4.87 seconds with 17 queries.