在JavaScript中迴圈使用執行相同的代碼塊指定的次數或在指定的條件是真實的。
範例
While
如何寫while迴圈。使用while迴圈運行相同的代碼塊,而指定的條件是真實的。
html>
<body>
<script type="text/javascript">
i=0;
while (i<=5)
{
document.write("The number is " + i);
document.write("<br />");
i++;
do...while Loop文法.
do
{
code to be executed
}
while (var<=endvalue);
好下面我們來看看while的執行個體.
<html>
<body>
<script type="text/javascript">
var i=0;
while (i<=10)
{
document.write("The number is " + i);
document.write("<br />");
i=i+1;
}
</script>
</body>
</html>結果.The number is 0
The number is 1
The number is 2
The number is 3
The number is 4
The number is 5
The number is 6
The number is 7
The number is 8
The number is 9
The number is 10再來看看do while執行個體.<html>
<body>
<script type="text/javascript">
var i=0;
do
{
document.write("The number is " + i);
document.write("<br />");
i=i+1;
}
while (i<0);
</script>
</body>
</html>結果為0