Home Web Front-end JS Tutorial Keyword examples of three js loops (for and while)_Basic knowledge

Keyword examples of three js loops (for and while)_Basic knowledge

May 16, 2016 pm 03:15 PM
for while

Three ways to write loops:

<!doctype html>
<title>js循环 by 脚本之家</title>
<meta charset="utf-8"/>
<meta name="keywords" content="js循环 by 脚本之家" />
<meta name="description" content="js循环 by 脚本之家" />
</head>
<body>
//while循环
 <script type="text/javascript">
i = 1;
while (i <= 6)
{
document.write("<h" + i+">脚本之家,这是标题"+i);
document.write("</h"+i+">");
i++;
}
</script>
//do_whilel循环
<script type="text/javascript">
i = 1;
do
{
document.write("<h" + i+">jb51.net ,这是标题"+i);
document.write("</h"+i+">");
i++;
}
while(i<=6);
</script>
//for循环 
 <script type="text/javascript">
for(i=1;i<=6;i++)
{
 document.write("<h"+i+">脚本之家,这是标题"+i);
 document.write("</h"+i+">");
}
</script>
</body>
</html>
Copy after login


Different types of loops

JavaScript supports different types of loops:
•for - loop through a block of code a certain number of times
•for/in - Loop through the properties of an object
•while - Loop through the specified block of code when the specified condition is true
•do/while - also loops through the specified code block when the specified condition is true


For loop

The for loop is a tool you will often use when you want to create a loop.

The following is the syntax of the for loop:

for (statement 1; statement 2; statement 3)
{
The executed code block
}


Statement 1 is executed before the loop (code block) starts
Statement 2 defines the condition for running the loop (block of code)
Statement 3 is executed after the loop (block of code) has been executed

Example

Copy code The code is as follows:

for (var i=0; i<5; i++)
{
x=x + "The number is " + i + "
";
}

Try it for yourself

From the example above, you can see:

Statement 1 sets the variable (var i=0) before the loop starts.
Statement 2 defines the conditions for the loop to run (i must be less than 5).
Statement 3 increments a value (i++) each time the block of code has been executed.


Statement 1

Usually we use statement 1 to initialize the variables used in the loop (var i=0).

Statement 1 is optional, which means you can do it without using statement 1.

You can initialize any (or multiple) values ​​in statement 1:

Example:

Copy code The code is as follows:

for (var i=0,len=cars.length; i {
document.write(cars[i] + "
");
}

You can also omit statement 1 (for example, when the value has been set before the loop starts):

Example:

Copy code The code is as follows:

var i=2,len=cars.length;
for (; i {
document.write(cars[i] + "
");
}

Statement 2

Usually statement 2 is used to evaluate the condition of the initial variable.

Statement 2 is also optional.
If statement 2 returns true, the loop starts again, if it returns false, the loop ends.
Tip: If you omit statement 2, you must provide a break inside the loop. Otherwise the cycle cannot be stopped. This may crash the browser. Please read about break later in this tutorial.

Statement 3

Normally statement 3 will increase the value of the initial variable.

Statement 3 is also optional.
Statement 3 can be used in several ways. The increment can be negative (i--), or larger (i=i+15).
Statement 3 can also be omitted (for example, when there is corresponding code inside the loop):

Example:

Copy code The code is as follows:

var i=0,len=cars.length;
for (; i {
document.write(cars[i] + "
");
i++;
}

For/In loop

JavaScript for/in statement loops through the properties of an object:

Example

Copy code The code is as follows:

var person={fname:"John",lname:"Doe",age:25};
for (x in person)
{
txt=txt + person[x];
}

You'll learn more about for / in loops in the chapter on JavaScript objects.

For details, please refer to this article: http://www.jb51.net/w3school/js/js_loop_for.htm

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

17 ways to solve the kernel_security_check_failure blue screen 17 ways to solve the kernel_security_check_failure blue screen Feb 12, 2024 pm 08:51 PM

Kernelsecuritycheckfailure (kernel check failure) is a relatively common type of stop code. However, no matter what the reason is, the blue screen error causes many users to be very distressed. Let this site carefully introduce 17 types to users. Solution. 17 solutions to kernel_security_check_failure blue screen Method 1: Remove all external devices When any external device you are using is incompatible with your version of Windows, the Kernelsecuritycheckfailure blue screen error may occur. To do this, you need to unplug all external devices before trying to restart your computer.

In C language, what is the difference between while(1) and while(0)? In C language, what is the difference between while(1) and while(0)? Aug 31, 2023 am 10:45 AM

We know that in C language, 'while' keyword is used to define a loop that works based on the condition passed to the loop. Now, since the condition can have two values, true or false, the code inside the while block will be executed repeatedly if the condition is true and will not be executed if the condition is false. Now, by passing parameters to the while loop, we can differentiate between while(1) and while(0) because while(1) is a loop where the condition is always considered true and hence the code inside the block will start executing repeatedly. Furthermore, we can state that it is not 1 that is passed to the loop that makes the condition true, but if any non-zero integer is passed to the while loop, then it will be considered as the true condition, so

How to uninstall Skype for Business on Win10? How to completely uninstall Skype on your computer How to uninstall Skype for Business on Win10? How to completely uninstall Skype on your computer Feb 13, 2024 pm 12:30 PM

Can Win10 skype be uninstalled? This is a question that many users want to know, because many users find that this application is included in the default program on their computers, and they are worried that deleting it will affect the operation of the system. Let this website help users Let’s take a closer look at how to uninstall Skype for Business in Win10. How to uninstall Skype for Business in Win10 1. Click the Windows icon on the computer desktop, and then click the settings icon to enter. 2. Click "Apply". 3. Enter "Skype" in the search box and click to select the found result. 4. Click "Uninstall". 5

How to use for to find the factorial of n in JavaScript How to use for to find the factorial of n in JavaScript Dec 08, 2021 pm 06:04 PM

How to use for to find n factorial: 1. Use the "for (var i=1;i<=n;i++){}" statement to control the loop traversal range to "1~n"; 2. In the loop body, use "cj *=i" Multiply the numbers from 1 to n, and assign the product to the variable cj; 3. After the loop ends, the value of the variable cj is the factorial of n, and then output it.

What is the difference between foreach and for loop What is the difference between foreach and for loop Jan 05, 2023 pm 04:26 PM

Difference: 1. for loops through each data element through the index, while forEach loops through the data elements of the array through the JS underlying program; 2. for can terminate the execution of the loop through the break keyword, but forEach cannot; 3. for can control the execution of the loop by controlling the value of the loop variable, but forEach cannot; 4. for can call loop variables outside the loop, but forEach cannot call loop variables outside the loop; 5. The execution efficiency of for is higher than forEach.

Usage of while Usage of while Sep 25, 2023 am 09:47 AM

The usage of while is "while condition: code block". The condition is an expression. When the condition is true, the code block is executed, and then it is judged again whether the condition is true. If it is true, the code block continues to be executed until the condition is false. . while is a commonly used loop control statement, used to repeatedly execute a block of code when certain conditions are met.

Is while a keyword in go language? Is while a keyword in go language? Jun 04, 2021 pm 05:01 PM

In the Go language, while is not a keyword. You can use the for statement plus break to achieve the effect of a while loop, such as "for {sum++ if sum>10{break}else{...}}". The go language has 25 keywords such as break, default, func, select, case, defer, go, map, else, goto, for, if, var, etc.

Detailed explanation of the function and usage of break keyword in PHP Detailed explanation of the function and usage of break keyword in PHP Jun 28, 2023 pm 06:39 PM

Detailed explanation of the role and usage of the break keyword in PHP In PHP programming, break is a control flow statement used to interrupt the current loop or switch statement and jump out of the loop or switch. This article will introduce in detail the role and usage of the break keyword. 1. Break in a loop In a loop structure, the function of break is to terminate the loop early and jump out of the loop body to execute the code after the loop. Common loop structures include for, while and do...while. in for loop

See all articles