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时执行语句块 }
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!