The while Statement of C #

Source: Internet
Author: User
Tags continue expression

The while statement conditionally executes the inline statement 0 times or several times. The format of the statement is:

while (boolean-expression) embedded-statement

The order in which it is executed is:

(1) Computes the value of the Boolean expression boolean-expression.
(2) When the value of the Boolean expression is true, execute the inline statement embedded-statement, and the program moves to step 1th;
(3) When the value of the Boolean expression is false, the while loop ends;

Let's look at a simple example that finds a specified value in an array, returns an array subscript if found, or returns and reports:

Program Listing 8-3:

Using System;
Class Test
{
  static int find (int value,int[] array)
   {
    int i=0;
    while (Array[i]!=value) {
     if (++i>array). Length)
       Console.WriteLine ("Can not find");
   }
   return i;
  }
  static void Main () {
    Console.WriteLine (3,new int[] {5,4,3,2,1});
   }

The while statement allows you to use a break statement to end a loop, execute a subsequent statement, or use a continue statement to stop the execution of the inline statement and continue with the while loop.

We use the following program fragment to compute the factorial value of an integer x:

Long Y=1;

while (true)
{
  y*=x;
  x--;
  if (x==0) {break
    ;
  }
}
Related Article

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.