Home > Article > Web Front-end > What are the three methods of JavaScript looping?
Three loop methods of js: 1. while loop, syntax "while (conditional expression) {statement block}"; 2. "do-while" loop, syntax "do{statement block}while( Conditional expression)"; 3. for loop, syntax "for (variable initialization; conditional expression; variable update) {statement block}".
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
When we use JavaScript, we often encounter the need to run the same code over and over again. This is a waste of time and inefficient. Using loops is a wise choice, which greatly improves efficiency and reduces code. quantity.
There are three types of loops in JS:
1, while loop
2, do-while loop
3, for loop
1. Grammatical structure of while loop:
while(条件表达式){ 当条件表达式为布尔值true时要执行的语句块 }
2. Application of while loop
while Loops are often used in situations where you don't know the number of loops, such as asking the user to loop through an integer until a special character is entered. You have no way of knowing the number of times the loop will occur. For example:
#1. The grammatical structure of do…while:
do{ 条件表达式为true时执行的语句块 }while(条件表达式)
2. Application of do...while
The difference between do-while and while loop is that it first executes the statement in the loop, and then determines whether the expression is true. If it is If true, the loop continues; if false, the loop is terminated. Therefore, the do-while loop must execute the loop statement at least once. As follows:
for(变量初始化;条件表达式; 变量更新){ 条件表达式为true时执行语句块 }2. Application of for loopThe for loop is mostly used in situations where the number of loops is relatively clear. It is the kind that can be seen at a glance how many times it needs to be looped. It is relatively intuitive. The first sentence of the for loop contains the initialization of the variable and ends the loop. Conditions and each updated value, the actual thing to be done is executed inside the loop body. For example, for(n=1;n3. Transformation of for loop
javascript advanced tutorial]
The above is the detailed content of What are the three methods of JavaScript looping?. For more information, please follow other related articles on the PHP Chinese website!