using loops in javascript

3
Using loops in JavaScript 6 Mar 2014 The main strength of any programming language is its ability to process blocks of code repeatedly. JavaScript, like other programming languages, provides us with loop. Statements placed inside loops are executed a set number of times... or even infinitely. Loops can be introduced by using any one of the three statements; for , while and do...while Here is the format of a for loop: for (Initialization statements; Condition; Updation statements) { ... statements ... } When the JavaScript interpreter comes across a for loop, it executes the initialization statements and then checks the condition. Only when the condition returns 'true', the interpreter enters the loop. After the first iteration, the updation statements are executed and then the condition is evaluated again. This continues till the condition returns 'false' and the loop stops. Displaying the 12 times table in an alert box var msg = ""; var res = "0"; for (var x = 1; x <= 12; x++) { res = 12 * x; msg = msg + "12 X " + x + " = " + res + "\n"; } alert(msg);

Upload: mohd-faizal

Post on 17-Sep-2015

230 views

Category:

Documents


1 download

DESCRIPTION

loops

TRANSCRIPT

Using loops in JavaScript

Using loops in JavaScript 6 Mar 2014The main strength of any programming language is its ability to process blocks of code repeatedly. JavaScript, like other programming languages, provides us with loop. Statements placed inside loops are executed a set number of times... or even infinitely.

Loops can be introduced by using any one of the three statements; for, whileanddo...while

Here is the format of aforloop:

for (Initialization statements; Condition; Updation statements)

{

...

statements

...

}

When the JavaScript interpreter comes across aforloop, it executes the initialization statements and then checks the condition. Only when the condition returns 'true', the interpreter enters the loop. After the first iteration, the updation statements are executed and then the condition is evaluated again. This continues till the condition returns 'false' and the loop stops.

Displaying the 12 times table in an alert box

var msg = "";

var res = "0";

for (var x = 1; x