Js--do while, for loop statements

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.