Usage of asp.net while and do while loop statements

Source: Internet
Author: User

① While statement

When the value of an expression is true, this statement executes a statement list. Control

The Boolean expression of the while statement is enclosed in parentheses, followed by the while keyword. Brackets

The following statements are executed when the Boolean expression value is true.

The following is an application example of the while statement:

 

Int I = 1;

While (I <6)

{

Response. Write ("I value:" + 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 tutorial </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 Tutorial: Label id = "messageLabel" runat = "server"/>
</Form>
</Body>
</Html>

② Do statement

This statement executes its nested statement zero or multiple times. If

If the expression value is false, no nested statement is executed. However, to ensure nesting

You can use a do statement to execute a statement at least once.

The do statement is followed by a nested statement followed by a while keyword.

The Boolean expression that controls the number of loop executions is behind the while keyword. Because cloth

The expression is evaluated after the nested statement is executed. Therefore, the nested statement is executed at least once.

.

The following is an application example of the do statement:

Int I = 1;

Do

{

Response. Write ("I value:" + 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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.