Do and loop structure
do{
Circulation body;
}while (cyclic conditions);
Show "Hello World" five times
var I=1;
while (I<5)
{
Alert ("Hello World")
i++;
}
var j=6;
do{
Alert ("Hello World")
j + +;
}while (j<=5);
The loop body is executed at least once, and the while loop may not execute at one time
Ask for factorial of 5, 5! =5*4*3*2*1
War jiecheng=1,num=5;
do{
Jiecheng=jiecheng*num;
num--;
}while (num>=1);
Alert ("jiecheng=" +jiecheng);
For loop
for (expression 1 loop variable initialization statement; expression 2 loop condition; expression 3 change loop variable statement)
{
Circulation body;
}
Ask for factorial, 5!
var jiecheng=1;
for (num=5;num>=1;num--)
{
Jiecheng=jiecheng*num;
}
Alert ("jiecheng=" +jiecheng);
Omitting an expression
for (Var i=1;i<=5;i++)
{
Alert ("Hello World");
}
Omit expression One
var I=1;
for (; i<=5;i++)
{
Alert ("Hello World");
}
Ellipsis expression two--dead loop
Omit expression Three
for (i=1;i<=5;)
{
Alert ("Hello World");
i++;
}
Nested loops
Print ********** on the page
for (Var i=1;i<=10;i++)
{
document.write ("*");
}
Print five lines of 10 stars
for (Var j=1;j<=5;j++)
{
for (Var i=1;i<=10;i++)
document.write ("*");
document.write ("<br/>");
}
The number of stars per row is the same as the number of rows
for (Var i=1;i<=5;i++)
{
for (Var j=1;j<=i;j++)
{
document.write ("*");
}
document.write ("<br/>");
}
Js--do while, for Loop statement