|
Some times you need to run a statement multiple
times rather than just once .Two looping statements are supported
by JavaScript to carry out this task. The two statements are for loop and while loop.
It is ideal for situations in which you
want a group of instructions to occur a specified number of times.
Syntax :
For ( condition ) {
Statement
}
Example :
For (
var counter = 1; counter < = 10; ++counter ) {
document.write(counter); }
In this exercise, you will learn FOR loop using Script tag.
In FOR loop, a variable count is declared
and set to a value of 0. Then a test is made to see whether
the value of count is less than or equal to w. The value of
count is incremented by 1 by the statement count++. The process
proceeds until the value of count is greater than w. Where
w is the value that you are going to enter.
Type any Number in the first text field
and Click the button "Click Me" to view the result in the second text field.
The while loop, is better suited to situations
in which the number of loops required is to be determined by an outside source.
Syntax :
While ( condition ) {
Statement
.. }
Example :
While ( counter < 100) {
If ( counter >= 99 ) {
document.write("Counter is less than equal to 99");
} }
In this exercise, you will learn While loop using Script tag.
JavaScript includes the while loop,this
while loop will be executed as long as the condition is true,
if the condition is false, the statements might not execute
at all. With each iteration, the loop increments n.
Type any Number in the first text field
and Click the button "Click Me" to view the result
in the second text field. The result gives the number of iterations in the while loop.

Copyrights : Layout Galaxy All Rights Reserved
No part of this tutorial may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, electrostatic, magnetic tape, mechanical or otherwise, without prior permission in writing from Layout Galaxy.
|
|
|