C # for statement

Source: Internet
Author: User
Tags continue

The For statement is the most Frequently used loop statement in C #. It is convenient to use a for account name in advance knowing the number of cycles. The format of the For statement is:

for (Initializer;condition;iterator) embedded-statement

Where the Initializer,condition,iterator three items are optional. The initializer is initialized for the cyclic control variable, and the loop control variable can have one or more (separated by commas), conditon for cyclic control conditions, or one or more statements; iterator change the value of the cyclic control variable by law.

Please note that initialization, cyclic control conditions, and loop control are optional. If you ignore the condition, you may have a dead loop that you want to exit with a jump statement (break or Goto).

for (;;) {
Break For some reason
}
The For statement is executed in the following order:
(1) Execute the initializer part (if any) in the order of writing, and assign the initial value to the cyclic control variable;
(2) test whether the condition of condition (if any) is satisfied;
(3) If no conditon items or conditions are met, then execute the embedded statement once, press iterator to change the value of the cyclic control variable, and return to the second step of execution;
(4) If the condition is not satisfied, the for loop terminates.

The following example is very simple, printing a number from 1 to 9, but it clearly shows how the for statement works.

for (int i=0;i<10;i++)
Console.WriteLine (i);

The For statement can be nested to help us do a lot of repetitive, regular work.

The following examples are used to print Yang Hui's triangles.

Program Listing 8-4:

Using System;
Class Test
{public
  static void Main ()
  {
    int[,] a=new int[5,5];
    A[0,0]=1;
    for (int i=1;i<=5;i++) {
       a[i,0]=1;
       A[i,i]=1;
       for (int j=1;j〈i;j++) {
          a[i,j]=a[i-1,j-1]+a[i-1,j];
       }
    }
    for (int i=0;i〈5;i++) {
       for (int j=0;j〈i;j++) {
          Console.WriteLine (' {0} ', A[i][j])
         }
       Console.WriteLine ();
}} The results of running the program are:

1
1 1
1 2
1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1

To take the factorial of an integer as an example, we can write the code like this:

for (long y=1;x>0;x--)
Y*=x;

In the same way, we can use the break and continue statements to coordinate with the logical expressions in the circular judgment statement to achieve the purpose of controlling the loop.

Still, for example, if you want to print a number from 0 to 9 other than 7, you can skip the print statement as long as the For loop executes to 7 o'clock.

for (int i=0;i<10;i++) {
if (i==7) continue;
Console.WriteLine (i);
}
}

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.