国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

詳解JavaScript的while循環的使用

2019-11-20 12:22:24
字體:
來源:轉載
供稿:網友

 在寫一個程序時,可能有一種情況,當你需要一遍又一遍的執行一些操作。在這樣的情況下,則需要寫循環語句,以減少代碼的數量。

JavaScript支持所有必要的循環,以幫助您在所有編程的步驟。
 while 循環

在JavaScript中最基本的循環是while循環,這將在本教程中學習討論。
語法

while (expression){  Statement(s) to be executed if expression is true}

while循環的目的是為了反復執行語句或代碼塊(只要表達式為true)。一旦表達式為假,則循環將被退出。
例子:

下面的例子說明了一個基本的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 stopped!");//--></script>

這將產生以下結果:

Starting LoopCurrent Count : 0Current Count : 1Current Count : 2Current Count : 3Current Count : 4Current Count : 5Current Count : 6Current Count : 7Current Count : 8Current Count : 9Loop stopped! 

 
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 stopped!");//--></script>

這將產生以下結果:

Starting LoopCurrent Count : 0Loop stopped! 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 大理市| 伊吾县| 东莞市| 三亚市| 甘洛县| 齐齐哈尔市| 南皮县| 大名县| 内黄县| 绥滨县| 大丰市| 浪卡子县| 富裕县| 新源县| 桐城市| 陆河县| 南开区| 梁山县| 桂林市| 临桂县| 巢湖市| 包头市| 布尔津县| 茌平县| 沾益县| 繁昌县| 龙胜| 天等县| 岳西县| 德兴市| 富顺县| 新宾| 邵阳市| 龙海市| 古田县| 襄城县| 海阳市| 永兴县| 沽源县| 曲阜市| 英吉沙县|