December 23 Loop statement (for)

Source: Internet
Author: User

---restore content starts---

First, the features of the FOR Loop statement:

1. Initial conditions: The first state of the cycle.

2, Cycle conditions: Under what conditions, not satisfied is terminated.

3, state change: Change the value of the loop variable.

4. Loop body: The part to be executed repeatedly.

All for loop instructions are inseparable from these.

Second, for the form and use of the loop:

The form of a loop:

1, (1), for (initial condition ; cyclic condition ; State change)

int i;
for (i = 1; i <=; i++)
{
Console.Write (i+ "\ t");
}//Result output number 1 to 100

(2), for (; cycle conditions ;)

int i=1;
for (; I <= 100;)
{
Console.Write (i+ "\ t");

i++;
}//result is also output number 1 to 100

(3), for (;;)

int i=1;
for (;;)
{
Console.Write (i+ "\ t");

i++;
}//program is a dead loop program

Use of loops:

1. Find the number within 100 and 7 related. (Can be divisible by 7, single digit is 7, 10 digits is 7)
for (int i=1;i<=100;i++)
{
if (i% 7 = = 0 | | I% = = 7 | | i/10 = = 7)//Key
{
Console.Write (i + "t");
}
}
2. Display the ASCII code of the computer:
for (int i=0;i<=125;i++)
{
Console.Write (i+ "=" + (char) i+ "\ t");
}

Three, the attention matters:

1. The parentheses behind the for is separated by semicolons by three items.

2, for the back without a semicolon .

3. Do not write the cycle of death.

Iv. Nesting for loops:

int i,j;
for (i = 1; I <= 5; i++)
{
for (j = 1; J <= 5; j + +)
{
Console.Write ("*");
}
Console.WriteLine ();
}//Program output 25 *, horizontal 5, vertical 5.

---restore content ends---

First, the features of the FOR Loop statement:

1. Initial conditions: The first state of the cycle.

2, Cycle conditions: Under what conditions, not satisfied is terminated.

3, state change: Change the value of the loop variable.

4. Loop body: The part to be executed repeatedly.

All for loop instructions are inseparable from these.

Second, for the form and use of the loop:

The form of a loop:

1, (1), for (initial condition ; cyclic condition ; State change)

int i;
for (i = 1; i <=; i++)
{
Console.Write (i+ "\ t");
}//Result output number 1 to 100

(2), for (; cycle conditions ;)

int i=1;
for (; I <= 100;)
{
Console.Write (i+ "\ t");

i++;
}//result is also output number 1 to 100

(3), for (;;)

int i=1;
for (;;)
{
Console.Write (i+ "\ t");

i++;
}//program is a dead loop program

Use of loops:

1. Convert the number into ASCII code;

Three, the attention matters:

1. The parentheses behind the for is separated by semicolons by three items.

2, for the back without a semicolon .

3. Do not write the cycle of death.

Iv. Nesting for loops:

int i,j;
for (i = 1; I <= 5; i++)
{
for (j = 1; J <= 5; j + +)
{
Console.Write ("*");
}
Console.WriteLine ();
}//Program output 25 *, horizontal 5, vertical 5.

December 23 Loop statement (for)

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.