Pitch your project

Random text






Blogs

0    
By admin Category Uncategorized, Posted February 12th, 2016
Javascript For Loop

What is a Loop? A loop is a sequence of instructions that is continually repeated until a certain condition is reached. What kind of Loops do exist? JavaScript supports the following loops:
* for
* while
* do/while

A for-loop loops through a block of code a specified number of times. A while-loop loops through a block of code while a specified condition is true and a do/while-loop loops also through a block of code while specified condition is true.

For Loop

The for loop is usually used when you know in advance how many times a loop should be executed. You obviously do not follow this rule strictly. This is how a for-loop looks like:

For (var count=0; count< =10; count++) {
    Console.log("Variabele count: "+ count);
}

This is the output of the code above:

Variable count: 0
Variable count: 1
Variable count: 2
Variable count: 3
Variable count: 4
Variable count: 5
Variable count: 6
Variable count: 7
Variable count: 8
Variable count: 9
Variable count: 10

How does the For Loop work?

// start value loop condition increment
For( var variable = 0; variable <= 10; variable++ ) {
    //Here comes the code that runs a number of times.
}

As you see the For-Loop has a start value, a loop condition and the ++ increment. The start value is the value where the loop starts counting. The loop condition must return true to keep the loop running. The ++ increment means +1. As we see above the count variable has a start value of 0. The loop condition has the condition <= 10 (= what means equal to and < means lower than). The ++ increment is counting until the start value(10) is reached. Now the loop condition returns false and the loop will stop.

Posted by Oussama Laghmouchi

Leave a Reply

Your email address will not be published. Required fields are marked *