百分百源码网-让建站变得如此简单! 登录 注册 签到领金币!

主页 | 如何升级VIP | TAG标签

当前位置: 主页>网站教程>JS教程> JavaScript中轮回知识的介绍(代码示例)
分享文章到:

JavaScript中轮回知识的介绍(代码示例)

发布时间:09/01 来源:未知 浏览: 关键词:

本篇文章给大家带来的内容是关于JavaScript中轮回知识的介绍(代码示例),有必然的参照 价值,有需要的伴侣可以参照 一下,但愿对你有所帮忙。

for 轮回

在足本的运转次数已肯定的状况下使用 for 轮回。

语法:

for (变量=开端值;变量<=完毕值;变量=变量+步进值) 
{
    需施行的代码
}

实例:

说明:下面的例子定义了一个轮回程序,这个程序中 i 的起始值为 0。每施行一次轮回,i 的值就会累加一次 1,轮回会不断运转下去,直到 i 等于 10 为止。

注释:步进值可认为负。假如步进值为负,需要调整 for 声明中的比力运算符。

<html>
<body>

<script>
var i=0
for (i=0;i<=10;i++)
{
document.write("The number is " + i)
document.write("<br />")
}

</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

while 轮回

while 轮回用于在指定前提为 true 时轮回施行代码。

语法:

while
(变量<=完毕值)
{
    需施行的代码
}

留意:除了<=,还可以使用其他的比力运算符。

实例:

说明:下面的例子定义了一个轮回程序,这个轮回程序的参数 i 的起始值为 0。该程序会重复运转,直到 i 大于 10 为止。每次运转i的值会增添 1。

<html>
<body>
<script>
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 轮回

do...while 轮回是 while 轮回的变种。该轮回程序在初次运转时会第一施行一遍其中的代码,然后当指定的前提为 true 时,它会连续这个轮回。所以可以这么说,do...while 轮回为施行至少一遍其中的代码,即便前提为 false,由于其中的代码施行后才会停止前提验证。

语法:

do
{
    需施行的代码
}
while
(变量<=完毕值)

实例:

<html>
<body>
<script>
var i=0
do 
{
document.write("The number is " + i)
document.write("<br />")
i=i+1
}
while (i<0)

</script>
</body>
</html>

结果:

The number is 0

以上就是JavaScript中轮回知识的介绍(代码示例)的具体内容,更多请关注百分百源码网其它相关文章!

打赏

打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

百分百源码网 建议打赏1~10元,土豪随意,感谢您的阅读!

共有151人阅读,期待你的评论!发表评论
昵称: 网址: 验证码: 点击我更换图片
最新评论

本文标签

广告赞助

能出一分力是一分吧!

订阅获得更多模板

本文标签

广告赞助

订阅获得更多模板