详解JavaScript的while循环的使用
网络编程 2021-07-04 21:48www.168986.cn编程入门
这篇文章主要介绍了详解JavaScript的while循环的使用,是JS入门学习中的基础知识,需要的朋友可以参考下
在写一个程序时,可能有一种情况,当你需要一遍又一遍的执行一些操作。在这样的情况下,则需要写循环语句,以减少代码的数量。
JavaScript支持所有必要的循环,以帮助您在所有编程的步骤。
while 循环
在JavaScript中最基本的循环是while循环,这将在本教程中学习讨论。
语法
while (expression){ Statement(s) to be executed if expression is true }
while循环的目的是为了反复执行语句或代码块(只要表达式为true)。一旦表达式为假,则循环将被退出。
例子:
狼蚁网站SEO优化的例子说明了一个基本的while循环:
<script type="text/javascript"> <!-- var count = 0; document.write("Starting Loop" + "<br />"); while (count < 10){ document.write("Current Count : " + count + "<br />"); count++; } document.write("Loop sped!"); //--> </script>
这将产生以下结果:
Starting Loop Current Count : 0 Current Count : 1 Current Count : 2 Current Count : 3 Current Count : 4 Current Count : 5 Current Count : 6 Current Count : 7 Current Count : 8 Current Count : 9 Loop sped!
do...while 循环:
do...while loop 类似于while循环,不同之处在于条件检查发生在循环的末端。这意味着,在循环将总是至少执行一次,即使条件为假。
语法
do{ Statement(s) to be executed; } while (expression);
注意在do... while循环的末尾使用分号。
例子:
如在上面的例子中编写一个使用do... while循环程序。
<script type="text/javascript"> <!-- var count = 0; document.write("Starting Loop" + "<br />"); do{ document.write("Current Count : " + count + "<br />"); count++; }while (count < 0); document.write("Loop sped!"); //--> </script>
这将产生以下结果:
Starting Loop Current Count : 0 Loop sped!
编程语言
- 机械手焊接机器人编程 机械手焊接设备编程
- vb数据库编程实例,vb做数据库
- tiobe世界编程语言排行榜,全球编程语言排行
- 成都java编程培训 成都it编程培训中心
- 编程基础培训,编程基础知识培训
- flash游戏编程基础教程,flash游戏开发教程
- 不会英语能学编程吗 不会英语可以学程序员吗
- 爱心代码编程C语言 爱心代码编程c语言公式
- c语言编程学习入门 c语言编程快速入门
- 西门子plc编程培训 西门子plc编程培训班
- 哪里可以学编程 台州哪里可以学编程
- 少儿编程哪个好 少儿编程哪个品牌好
- 编程是什么意思,图形编程是什么意思
- c语言编程软件下载,c语言编程软件在哪下载
- java编程软件下载,java编程下载手机版
- ug编程培训学校,ug编程培训教程