JavaScript中while、do…while、for循环

The loop statement is simple and basic loop, while, do...while, for loop.
This article briefly talks about the three loops.
while loop
while first judges and then executes;
syntax: while ( expression ) { expression==ture/false (the code in curly braces will run repeatedly until it is false and exit )}

		//while循环案例
//		 1.循环输出10个“*”	
//		var i = 0;
//		while(i < 10){	
//			document.write("*");
//			i++;
//		}

do while loop The
do while loop will execute first, then judge, the body of the loop will be executed at least once
**The difference with the while loop is that the while loop is judged first and then executed, the body of the loop may not loop once .

		//04-dowhile循环
		
//		var i = 0;
//		while(i > 10){
//			console.log(i);
//			i++;
//		}
		
		//do//做
//		var i = 0;
//		do{
//			console.log(i);//0
//			i++;
//		}while(i > 10);
		

**


for loop
1. Three statements must be separated by two semicolons
. 2. The first statement initializes the variables used in the loop, which can be no variable or any variable (var i=0)
3. Whether the initial variable of the second statement can be Judgment condition for loop operation, the result of the condition is true to run, false to jump out of the loop, optional (i<10)
4. The value operation of the third statement initial variable after the loop, optional (i++)

5. for (initialization expression 1; Judgment expression 2; Self-increment and self-decrement expression 3) { // Loop body 4

example: output a number from 0-9

for (i = 0 ; i<10;i++) {
		console.log(i)
	}//i = 0 开始为ture,传递到console.log里然后i开始做自增运算,
	++到10之后为false,直接跳转出来。
	结果为//0 1 2 3 4 5 6 7 8 9

Guess you like

Origin blog.csdn.net/wzszq125/article/details/107283734