for循环
<!DOCTYPE html>
<html>
<head><meta charset="UTF-8"><title>首页</title>
</head><body><script type="text/javascript">var num = 100 for (let i = 0; i < num; i++){console.log(i)}</script></body></html>
循环嵌套,如果要将结果打印在页面上使用 document.write
while循环
<!DOCTYPE html>
<html>
<head><meta charset="UTF-8"><title>首页</title>
</head><body> <script type="text/javascript">let i = 0while(i <= 10){document.write(i + "<br>")i ++}</script></body></html>
break和continue
break和continue都有跳出语句的功能。break是跳出整个循环,continue是跳出当前本次循环。
当i的值为3的时候就跳出循环,这个循环就结束了。
<!DOCTYPE html>
<html>
<head><meta charset="UTF-8"><title>首页</title>
</head><body> <script type="text/javascript">for(let i = 1; i <= 10; i++){if (i == 5){document.write("本次循环结束" + "<br>")continue}document.write(i + "<br>")}</script></body></html>