JavaScript-5循环

JavaScript-5循环

       For循环

              例一:

                     <html>

<body>

<script type="text/javascript">

for (i = 0; i <= 5; i++)

{

document.write("The number is " + i)

document.write("<br />")

}

</script>

</body>

</html>

实图:

 

              例二:

                     <html>

<body>

<script type="text/javascript">

for (i = 1; i <= 6; i++)

{

document.write("<h" + i + ">标题 " + i+"</h" + i + ">")

}

</script>

</body>

</html>

                     实图:

 

              While循环

                     例一:

                            <html>

<body>

<script type="text/javascript">

i = 0

while (i <= 5)

{

document.write("The number is " + i)

document.write("<br>")

i++

}

</script>

</body>

</html>

实图:同for例一。

              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>

实图:

 

              Break 语句break终止循环的运行,然后执行循环之后的代码

                     <html>

<body>

<script type="text/javascript">

var i=0

for (i=0;i<=10;i++)

{

if (i==3){break}

document.write(i+"&nbsp;")

}

</script>

</body>

</html>

结果为:0 1 2

              Continue语句:终止当前循环,然后从下一个值继续运行。

                     <html>

<body>

<script type="text/javascript">

var i=0

for (i=0;i<=10;i++)

{

if (i==3){continue}

document.write(i+"&nbsp;")

}

</script>

</body>

</html>

结果为:0 1 2 4 5 6 7 8 9 10 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章