Javascript Loops

Submitted by: Submitted by

Views: 233

Words: 314

Pages: 2

Category: Science and Technology

Date Submitted: 04/25/2012 04:17 PM

Report This Essay

JavaScript Loops

Learner Outcomes

At the end of the lesson the student will be able to:

● Work with for loops

● Work with while loops

● Loop through the contents of an array

Looping refers to the ability of a block of code to repeat itself. JavaScript performs several types of repetitive operations. Using these operations you can iterate through the list of array elements, repeat certain operations until some condition is met, or just keep repeating a certain operation. Loops are also known as control structures. JavaScript offers three types of loop structures:

In this Lesson we talk about Loops. There are situations, where we need to repeat the same code many times for different data or values under certain conditions. Specially in arrays are loops very efficient. Loops are available in all programming languages. They probably have different syntax but it is the same idea and functionality.

In JavaScript there are two kind of loops:

Count-controlled loops | for - loops through a block of code a specified number of times

Condition-controlled loops | while - loops through a block of code while a specified condition is true

1. The for Loop

The for loop is used when you know in advance how many times the script should run.

● The for statement provides a general-purpose loop

for (initialization; condition; iteration) {

statements to repeat

}

● initialization: a statement that defines the initial conditions of the loop

● condition: an expression that determines when to continue (or end) the loop

● iteration: a statement evaluated at the end of each loop iteration

● The statements to repeat are enclosed in the curly bra

Using the for Loop

● A common use of a for loop is to count

● You use a counter variable to track number of times to run the block of statements

For example:...