Loops in JavaScript - PowerPoint PPT Presentation

1 / 9
About This Presentation
Title:

Loops in JavaScript

Description:

for(var i=1;i =X;i ) code. The number of times loop is repeated is the same. ... var i=0; while (i 6) document.write('The number is ' i); document.write ... – PowerPoint PPT presentation

Number of Views:125
Avg rating:3.0/5.0
Slides: 10
Provided by: pratik1
Category:
Tags: javascript | loops | var

less

Transcript and Presenter's Notes

Title: Loops in JavaScript


1
Loops in JavaScript
  • Pratik Desai

2
The FOR loop
  • Loop used to run the same piece of code again and
    again.
  • FOR loop runs a code specific number of times.
  • Used when we know in advance, how many times the
    script should run.

3
The FOR loop..
  • Syntaxfor(initializationconditioniteration)
    code to be executed
  • Initialization of counter, which counts the
    number of times the loop is run, i.e. the code is
    executed.
  • Iteration incrementing the counter by one..
  • Incrementing gt ii1 or i

4
The FOR loop
  • e.g. var total0var i0for (i0ilt6i)
    document.write("The number is " i)
    document.write("ltbr /gt")totaltotali
    document.write(The Total is total)
  • ResultThe number is 0 The number is 1 The
    number is 2 The number is 3 The number is 4
    The number is 5 The Total is 15

5
The FOR Loop
  • Two ways to write the loop..
  • for(var i0iltXi)code
  • for(var i1iltXi)code
  • The number of times loop is repeated is the same.
  • Output may differ though...

6
The WHILE Loop
  • Used when you want the loop to execute and
    continue executing while the specified condition
    is true. 
  • Syntaxwhile (condition) code to be executed

7
The WHILE Loop
  • e.g.var i0 while (ilt6) document.write("The
    number is " i) document.write("ltbr
    /gt")ii1
  • ResultThe number is 0The number is 1The
    number is 2The number is 3The number is 4The
    number is 5

8
The DOWHILE Loop
  • Variant of WHILE loop.
  • Executed at least once (even though condition is
    false), and then repeat the loop as long as
    specified condition is true.
  • Syntaxdocode to be executedwhile
    (condition)

9
The DOWHILE Loop
  • e.g.var i0do document.write("The number
    is " i) document.write("ltbr /gt")ii1
    while (ilt0)
  • ResultThe number is 0
  • Notice that loop executed once even though
    condition is false.
Write a Comment
User Comments (0)
About PowerShow.com