Ticker

6/recent/ticker-posts

JavaScript (Loop)

INTRODUCTION OF LOOP

Loops are also known as iterative statement because its repeat itself until its given condition and in JavaScript loop can be classify in the following categories.


TYPES OF LOOP IN JAVASCRIPT


1. While Loop

2. Do - While Loop

3. For Loop


WHILE LOOP
The while loop first check its test condition and then execute its body part.
Note:Every loop consisted with number of sub parts, which are describe below.
1. Starting  statement: This part responsible to initialize the loop's variable
Example
var a=1;
In the above code 'var' is a reserve keyword that is used to declare a variable in JavaScript.
 'a' is name of variable and '1' its starting value.

2. Test condition: The test responsible to stop or terminate the loop's process.
Example
while(a <= 10)
In the above program code 'while' is a reserve JavaScript keyword that is used to define while loop and '(a <= 10) its terminate condition.

3. Increment Or Decrement: These statements are used to change the value of loop's variable. 
Example
a++
a=a+1
Note: In the above code a incremented by one.

WHILE LOOP EXAMPLE
<html>
<title> Print 1 - 10 counting by while loop  </title>

<body bgcolor=yellow text=red>
<form>
<input type=button value="Counting" onclick="count()">
</form>

<script>
function count()
{
var a=1;
while(a <= 10)
{
document.write("<Font size=6> <br>",a);
a=a+1;
}

}
</script>
</html>

PROGRAM TO PRINT COUNTING UNTIL GIVEN NUMBER
<html>
<title> Print counting  </title>
<body bgcolor=yellow text=red>
<form name=f>
Enter any number <input type=text name="n">
<input type="button" value="Result" onclick="count()">
</form>

<script>
function count()
{
var a=1;
var n=f.n.value;
while(a <= n )
{
document.write("<body bgcolor=yellow text=red><Font
size=6>",a,"<br>");
a=a+1;
}
}
</script>
</html>


DO WHILE LOOP
The do while loop first execute its body contents then check its test condition.
Therefore do while loop execute atleast once even its test condition would be false.



DIFFERENCE BETWEEN WHILE AND DO WHILE LOOP
The main difference between while and do while loop is that, while loop never execute, when its test condition would be false but the do while loop execute atleast once even its test condition would be false.


DO WHILE LOOP EXAMPLE
<html>
<title> Print 1-10 counting by do while loop </title>
<body>
<script>
var a=1;

do
{
document.write(a,"<br>");
a++;
}
while(a <=10);
</script>
</body>
</html>


Program to print reverse counting from given number to 0
<html>
<title> Print Reverse Counting </title>
<body>

<form name=f>
Enter any number <input type=text name=num>
<input type=button value=Submit onclick="Rcount()">
</form>

<script>
function Rcount()
{
var n=f.num.value;
var c=Number(n);
do
{
document.write(c,"<br>");
c--;
}
while(c >= 0);
}
</script>
</body>
</html>



For Loop
The for loop is a simplest loop among all types of loops, because its contain all loop's element (Initialling, Test Condition, Increment or Decrement) in just a single statement.

EXAMPLE: Print 1-10 counting by for loop
<html>
<title> A simple for loop example </title>
<body>

<script>
var a;
for(a=1; a<=10; a++)
{
document.write(a,"<br>")
}
</script>

</body>
</html>

Program to print maths table for a given number
<html>
<title> Print maths table for a given value </title>
<body>
<form name=f>
Enter any number <input type=text name=num size=5>
<input type=button value=submit onclick=mt()>
</form>

<script>
function mt()
{
var n=Number(f.num.value);
var i;
for(i=1; i<=10; i++)
{
document.write(n*i,"<br>");
}
}
</script>
</body>
</html>






HOME




Post a Comment

0 Comments