<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          【JavaScript 教程】第四章 程序流程04— while 循環(huán)語(yǔ)句

          共 1618字,需瀏覽 4分鐘

           ·

          2021-11-27 10:45

          英文 | https://www.javascripttutorial.net/

          譯文 | 楊小愛(ài)


          在上節(jié)中,我們學(xué)習(xí)了JavaScript 程序流程中的switch語(yǔ)句,錯(cuò)過(guò)的小伙伴可以點(diǎn)擊文章《【JavaScript 教程】第四章 程序流程03— switch 語(yǔ)句》進(jìn)行學(xué)習(xí)。

          那么,在今天的教程中,我們將一起來(lái)學(xué)習(xí)如何使用 JavaScript while 語(yǔ)句創(chuàng)建循環(huán)。

          JavaScript while 循環(huán)語(yǔ)句簡(jiǎn)介

          JavaScript  while語(yǔ)句創(chuàng)建一個(gè)循環(huán),只要測(cè)試條件的計(jì)算結(jié)果為true,就會(huì)執(zhí)行代碼塊。
          下面是while 語(yǔ)句的語(yǔ)法說(shuō)明示例。
          while (expression) {    // statement}

          while 語(yǔ)句在每次循環(huán)迭代之前計(jì)算表達(dá)式。

          如果表達(dá)式的計(jì)算結(jié)果為真,while 語(yǔ)句將執(zhí)行該語(yǔ)句。如果表達(dá)式的計(jì)算結(jié)果為 false,則繼續(xù)執(zhí)行 while 循環(huán)之后的語(yǔ)句。

          while 循環(huán)在每次迭代之前計(jì)算表達(dá)式,因此,while 循環(huán)稱(chēng)為預(yù)測(cè)試循環(huán)。由于這個(gè)原因,while 循環(huán)中的語(yǔ)句可能永遠(yuǎn)不會(huì)被執(zhí)行。

          以下流程圖說(shuō)明了 while 循環(huán)語(yǔ)句:

          請(qǐng)注意,如果要執(zhí)行該語(yǔ)句至少一次,并在每次迭代后檢查條件,則應(yīng)使用do-while語(yǔ)句。

          JavaScript while 循環(huán)示例

          請(qǐng)參閱以下使用該while語(yǔ)句的示例:

          let count = 1;while (count < 10) {    console.log(count);    count +=2;}

          它的工作原理

          首先,在循環(huán)之外,計(jì)數(shù)變量設(shè)置為 1。

          其次,在第一次迭代開(kāi)始之前,while 語(yǔ)句會(huì)檢查 count 是否小于 10 并執(zhí)行循環(huán)體內(nèi)的語(yǔ)句。

          第三,在每次迭代中,循環(huán)將 count 增加 2,在 5 次迭代后,條件 count < 10 不再為true,因此循環(huán)終止。

          控制臺(tái)窗口中的腳本輸出如下:

          13579

          以下示例使用while循環(huán)語(yǔ)句將 0 到 10 之間的 5 個(gè)隨機(jī)數(shù)添加到數(shù)組中:

          // create an array of five random number between 1 and 10let rands = [];let count = 0;const size = 5;
          while(count < size) { rands.push(Math.round(Math.random() * 10)); count++; console.log('The current size of the array is ' + count);}
          console.log(rands);
          輸出:
          The current size of the array is 1The current size of the array is 2The current size of the array is 3The current size of the array is 4The current size of the array is 5
          [1, 9, 1, 9, 6]

          在這個(gè)例子中:

          • 首先,聲明并初始化一個(gè)數(shù)組。

          • 其次,在 while 語(yǔ)句內(nèi)的每次循環(huán)迭代中添加一個(gè) 0 到 10 之間的隨機(jī)數(shù)。如果計(jì)數(shù)值等于大小變量的值,則循環(huán)停止。

          總結(jié)

          通過(guò)本教程的學(xué)習(xí),我們知道了如何使用 JavaScript 的 while 語(yǔ)句創(chuàng)建一個(gè)預(yù)測(cè)試循環(huán),只要條件為真,該循環(huán)就會(huì)執(zhí)行代碼塊。


          學(xué)習(xí)更多技能

          請(qǐng)點(diǎn)擊下方公眾號(hào)

          瀏覽 55
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  麻豆精品无码久久久 | 成人AC视频 | 精品人妻91 | 一级福利在线播放 | 国产av激情无码 国产av最新福利 |