For、While 和 Do While 循环 Java示例脚本
⚡ 智能摘要
for、while 和 do while 循环 Java脚本可以重复执行一段语句块,而不会重复编写代码。本文将解释每种语法,展示可运行的示例及其预期输出,介绍 for-in 和 for-of 循环形式,并涵盖 break、continue 和无限循环预防等内容。

什么是循环? Java脚本?
Loops are useful when you have to execute the same lines of code repeatedly, for a specific number of times or as long as a specific condition is true. Suppose you want to type a 'Hello' message 100 times in your webpage. Of course, you will have to copy and paste the same line 100 times. Instead, if you use loops, you can complete this task in just 3 or 4 lines.
无论语法如何,每个循环都由相同的三个部分组成:起始值、决定是否执行下一轮循环的条件,以及使循环向结束移动的更新。下图展示了这个重复的循环。
不同类型的循环 Java脚本
主要有四种类型的循环 JavaScript.
- for循环
- for/in 循环
- while循环
- 做…while 循环
现代 Java脚本添加了 for…of 循环,可以直接迭代值,因此实际上有五种形式可供选择。下表显示了在特定情况下应该使用哪种形式。
| 循环 | 状况已检查 | 最低执行次数 | 最适合使用 |
|---|---|---|---|
| HPMC胶囊 | 每次传球前 | 0 | 已知重复次数 |
| 而 | 每次传球前 | 0 | 由某种条件驱动的未知计数 |
| 做…同时 | 每次传递后 | 1 | 必须运行一次的菜单和提示 |
| 对于……在 | 每次传球前 | 0 | 读取物体的键 |
| 为了…… | 每次传球前 | 0 | 读取数组或字符串的值 |
for循环
语法:
for(statement1; statement2; statment3)
{
lines of code to be executed
}
- 语句1会首先执行,甚至在执行loo之前。ping 代码。因此,这条语句通常用于为循环内部将要使用的变量赋值。
- statement2是执行循环的条件。
- 语句3在每次上厕所后都会执行。ping 代码已执行。
自己尝试一下:
<html>
<head>
<script type="text/javascript">
var students = new Array("John", "Ann", "Aaron", "Edwin", "Elizabeth");
document.write("<b>Using for loops </b><br />");
for (i=0;i<students.length;i++)
{
document.write(students[i] + "<br />");
}
</script>
</head>
<body>
</body>
</html>
输出:
Using for loops John Ann Aaron Edwin Elizabeth
计数器从 0 开始,因为第一个索引是 排列 为零,并且条件使用 students.length,因此当添加或删除姓名时,循环会自动调整。
while循环
语法:
while(condition)
{
lines of code to be executed
}
The "while loop" is executed as long as the specified condition is true. Inside the while loop, you should include the statement that will end the loop at some point of time. Otherwise, your loop will never end and your browser may crash.
自己尝试一下:
<html>
<head>
<script type="text/javascript">
document.write("<b>Using while loops </b><br />");
var i = 0, j = 1, k;
document.write("Fibonacci series less than 40<br />");
while(i<40)
{
document.write(i + "<br />");
k = i+j;
i = j;
j = k;
}
</script>
</head>
<body>
</body>
</html>
输出:
Using while loops Fibonacci series less than 40 0 1 1 2 3 5 8 13 21 34
⚠️警告: 如果 while 循环的条件始终为假,则会导致浏览器标签页卡死。务必确认循环体内的变量是否会使条件趋向于假,并且当循环的退出依赖于外部数据时,请使用安全计数器。
做…while 循环
语法:
do
{
block of code to be executed
} while (condition)
do…while 循环与 while 循环非常相似。唯一的区别在于,在 do…while 循环中,代码块会在检查条件之前执行一次。
自己尝试一下:
<html>
<head>
<script type="text/javascript">
document.write("<b>Using do...while loops </b><br />");
var i = 2;
document.write("Even numbers less than 20<br />");
do
{
document.write(i + "<br />");
i = i + 2;
}while(i<20)
</script>
</head>
<body>
</body>
</html>
输出:
Using do...while loops Even numbers less than 20 2 4 6 8 10 12 14 16 18
当条件从一开始就为假时,区别就显现出来了。while 循环不会打印任何内容,而 do…while 循环在停止之前仍然会打印一行。ping.
for…在循环中
for…in 循环遍历对象的可枚举属性名。每次遍历都会将下一个键赋值给循环变量,并使用方括号表示法读取其值。
const student = { name: "Ann", age: 21, course: "JavaScript" }; for (const key in student) { console.log(key + " : " + student[key]); }
输出:
name : Ann age : 21 course : JavaScript
💡提示: 避免在数组中使用 `for…in` 循环。它会将索引值作为字符串返回,并且还会访问继承的属性,这可能会导致意想不到的结果。请改用 `for…of` 循环或经典的 `for` 循环来处理数组。
for…循环和forEach
`for…of` 循环读取任何可迭代对象的值,包括数组、字符串、映射和集合。`forEach` 方法通过回调函数实现相同的功能,并允许访问索引。
const students = ["John", "Ann", "Aaron"]; // for...of reads each value directly for (const name of students) { console.log(name); } // forEach supplies the value and the index students.forEach(function (name, index) { console.log(index + " - " + name); }); // strings are iterable too for (const letter of "Ann") { console.log(letter); }
输出:
John Ann Aaron 0 - John 1 - Ann 2 - Aaron A n n
一个实际的区别很重要:可以在 for…of 内部中断并继续工作,但不能在 forEach 内部使用,因为每个回调都是一个单独的函数。
中断并继续语句
两个关键字会改变任何循环的正常流程。`break` 语句会完全退出循环,而 `continue` 语句会放弃当前循环并跳转到下一循环。
const numbers = [4, 7, 10, 13, 16, 19]; // continue: skip odd numbers for (const n of numbers) { if (n % 2 !== 0) { continue; } console.log("even: " + n); } // break: stop at the first value above 12 for (const n of numbers) { if (n > 12) { console.log("first value above 12: " + n); break; } }
输出:
even: 4 even: 10 even: 16 first value above 12: 13
循环几乎出现在你编写的每个脚本中。继续…… 实际 Java脚本代码示例重访 Java脚本数组方法 对于构建在这些循环之上的迭代辅助函数,并读取 内部和外部 Java脚本 决定最终剧本应该保存在哪里。

