In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of "how to use the loop sentence of javascript". The editor shows you the operation process through the actual case. The operation method is simple, fast and practical. I hope this article "how to use the loop sentence of javascript" can help you solve the problem.
The JavaScript loop statement is composed of two parts: the loop body and the termination condition. The loop body is the statement executed repeatedly, and the termination condition is the condition that determines whether it can be repeated or not. JavaScript loop statement: 1, for statement; 2, for in statement; 3, while statement; 4, do while statement.
The operating environment of this tutorial: windows10 system, javascript1.8.5 version, Dell G3 computer.
Loop statement of javascript
In many practical problems, there are many regular repetitive operations, so it is necessary to repeat some statements in the program.
A group of statements that are repeatedly executed are called loop bodies, and whether they can be repeated or not determines the termination conditions of the loop. The loop structure is the process structure that repeatedly executes a certain program under certain conditions, and the program that is executed repeatedly is called the loop body. The loop statement consists of two parts: the body of the loop and the termination condition of the loop. So let's introduce our commonly used loop statements.
JavaScript supports four different types of loops:
For: traversing the code block multiple times
For/in: traversing object properties
While: loop a block of code when the specified condition is true
Do/while: loop a block of code when the specified condition is true
For statement
The syntax format of the for statement is as follows:
For (statement 1; statement 2; statement 3) {Block of code to be executed}
Statement 1 is executed before the loop (code block) begins. Statement 1 is optional. If there is no statement, just write a semicolon. We usually initialize some variables in statement 1, which can be one or more, with multiple variables separated by commas.
Statement 2 defines the conditions under which the loop (code block) is run. If the condition is true, enter the loop and execute the block of code in the loop, otherwise, end the loop. Statement 2 is also optional, if there is no statement, write a semicolon directly, however, it should be noted that if there is no statement, you need to write a break in the body of the loop, otherwise, it will never end and become a dead loop.
Statement 3 is executed each time the loop (code block) is executed. Statement 3 is also optional, and the contents of statement 3 can be written in the loop body.
Note one small detail. There is no semicolon at the end of statement 3.
Write a small example:
Var text = "", I; for (I = 0; I < 5; iTunes +) {text + = "The number is" + I + "
";} document.getElementById (" demo ") [xss_clean] = text
Statement 1 sets a variable (var I = 0) before the loop starts.
Statement 2 defines the condition under which the loop is run (I must be less than 5).
Statement 3 increments the value after each execution of the code block (iSuppli +).
For/in statement
The for/in statement is generally used to traverse the properties of an object. The syntax format is as follows:
For (attribute name in object) {Block of code to execute}
Write a small example:
Var person = {name: "Liu Xiaoniu", sex: "female", work: "self-media"}; var x _ dint txt = ""; for (x in person) {txt + = person [x] + ";} document.getElementById (" demo ") [xss_clean] = txt
X stands for attribute name, which can be defined freely, similar to formal parameter. Access property values in the form of an object [property name], not an object. The form of property name.
While statement
The syntax format of while is as follows:
While (condition) {Block of code to be executed}
If the condition is true, the code block will be executed in a loop all the time.
Write a small example:
Var text = ""; var I = 0; while (I < 10) {text + = "
The number is "+ I; iTunes;} document.getElementById (" demo ") [xss_clean] = text
Note that if you are not writing an endless loop, be sure to have a statement in the body of the loop that will invalidate the condition.
For example, in the above example, there is an icycle in the body of the loop, and when I increments to 10, the loop ends.
Do/while statement
The syntax format of do/while is as follows:
Do {Code Block to be executed} while (condition)
The do/while loop is a variant of the while loop.
This loop executes the code block once before checking whether the condition is true, and then repeats the loop as long as the condition is true.
Write a small example:
Var text = "" var I = 0; do {text + = "
The number is "+ I; iTunes;} while (I < 10); document.getElementById (" demo ") [xss_clean] = text;. Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.