LOOPS IN JAVASCRIPT
Loops
JavaScript language supports following loops :
while :loops through a block of code while a specified
condition is true
do…while:also loops through a block of code while a spe-
cified
condition is true
for:loops through a block of code a number of times
for/of:loops through the values of an array
for/in: Newly introduced in ES6 and loops through the
properties of an object
Syntax Of while Loop
while
while(<test
cond>)
{
// loop body
}
PROGRAM
Write a JavaScript code to accept an integer
from the user and print the table of that no up
to 10 terms
Syntax Of do…while
Loop:
do…while
do
{
// loop body
}while(<test
cond>);
PROGRAM
Write a JavaScript code to accept an integer
from the user and calculate it’s factorial
PROGRAM
Write a JavaScript code to accept the birth year
from the user and calculate and print his age.
Also print whether the user can vote or not .
Make sure if the age turns out to be 0 or
negative your code prompts the user to input
his birth year again until the age comes
positive
Syntax Of for Loop
for
for
(stmt1;stmt2;stmt
3)
{
// loop body
}
Various forms of for loop :
Syntax:
for (stmt 1, stm2; stmt 3; stmt
4)
{
//the code block to be
executed
}
Various forms of for loop :
Syntax:
for ( ; stmt 2;stmt 3)
{
//the code block to be
executed
}
PROGRAM
Write a JavaScript code to accept an integer
from the user and calculate and print the sum
of even and sum of odd nos up to the given no
separately