asp.net while 與 do while迴圈語句的用法

來源:互聯網
上載者:User

①while語句

  當一個運算式的值為真時,這個語句執行一個語句列表。控制

while語句的布林運算式包含在括弧內,前面是while關鍵字。括弧

後面是當布林運算式值為真時應執行的語句。

  下面是while語句的一個應用執行個體:

 

  int i=1;

  while (i < 6 )

  {

   Response.Write(“I的值為:“+i.ToString().Trim()+

“<br>“);

   i++;

  }


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
  <head>
    <title>Loops教程</title>
    <script runat="server" language="C#">
      void Page_Load()
      {

        int counter = 0;

        while (counter <= 10)
        {

          messageLabel.Text = counter.ToString();

          counter++;
        }
      }
    </script>
  </head>
  <body>
    <form runat="server">
      <asp教程:Label id="messageLabel" runat="server"/>
    </form>
  </body>
</html>

  ②do語句

  這個語句執行其嵌套語句零次或多次。如果while語句中的布

爾運算式值為假,則不執行任何嵌套語句。但是,為了保證嵌套語

句至少執行一次,可以使用一個do語句。

  do語句後面是一個嵌套語句,後面緊跟著一個while關鍵字。

控制迴圈執行次數的布林運算式位於while關鍵字的後面。由於布

爾運算式在嵌套語句執行後再求值,因此嵌套語句至少被執行一次

  以下為do語句的一個應用執行個體:

  int i=1;

  do

  {

   Response.Write(“I的值為:“+i.ToString().Trim()+

“<br>“);

   i++;

  }while (i < 6);

do while

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
  <head>
    <title>Loops</title>
    <script runat="server" language="C#">
      void Page_Load()
      {

        int counter = 0;

        do
        {

          messageLabel.Text = counter.ToString();

          counter++;
        }
        while (counter <= 10);
      }
    </script>
  </head>
  <body>
    <form runat="server">
      <asp:Label id="messageLabel" runat="server"/>
    </form>
  </body>
</html>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.