C#for statements

Source: Internet
Author: User

Loop: You can execute a piece of code repeatedly until the loop condition is not met.
First, the four elements of the cycle: initial conditions, cyclic conditions, state changes, circulation body.
1. Initial conditions: The first state of the cycle.
2. Loop condition: Under what conditions the loop is performed and the condition is not satisfied, the loop terminates.
3. State change: Change the value of the cyclic variable, and ultimately do not meet the cycle conditions, thus stopping the loop.
4. Loop body: The part to be executed repeatedly.

Second, syntax: for syntax.//And While,do...whilte
for (initial condition; cyclic condition; state change)
{
Loop body
}
Attention:
In parentheses after 1.for, three items are separated by semicolons.
Do not add a semicolon after the 2.for parenthesis.
3. Do not write the cycle of death.
Cases:
for (int i=1;i<=10;i++)
{
Console.WriteLine ("Hello");
}

Third, the implementation process:
1. Performing Initial conditions
2. Implementing Cycle Conditions
3. Loop body
4. Status change
5 continue the 2nd step.


Iv. Examples of:
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");
}

For Loop variant:
Variant one: While loop
int i = 1;//initial condition
for (; I <= 100;)
{
Console.Write (i + "t");
i++;//State Change
}
Equivalent:
int i = 1;//initial condition
while (I <= 100)
{
Console.Write (i + "t");
i++;//State Change
}

Variant two: Dead loop
for (;;)
{
}

V. Nesting of loops.
for (int i=1;i<=10;i++)
{
for (int j=1;j<=20;j++)
{
Console.Write ("★");
}
Console.WriteLine ();
}
When the outer layer is cycled 1 times, the inner layers are completely cycled through.
Homework:
Print to type:
Row I column Ji and j relationship
The
-
*
$
-

for (int i = 1; I <= 5; i++)
{
for (int j = 1; J <= 5; j + +)
{
Console.Write ("#");
}
Console.WriteLine ();
}

J<=i
A
-
-
-

for (int i = 1; I <= 5; i++)
{
for (int j = 1; J <= I; j + +)
{
Console.Write ("#");
}
Console.WriteLine ();
}

J<=6-i
-
-
The
Wuyi

for (int i = 1; I <= 5; i++)
{
for (int j = 1; J <= 6-i; j + +)
{
Console.Write ("#");
}
Console.WriteLine ();
}


---141
---232
--323
-414
505

for (int i = 1; I <= 5; i++)
{
for (int j = 1; J <= (5-i); j + +)
{
Console.Write ("");
}
for (int k = 1; k <= i; k++)
{
Console.Write ("#");
}
Console.WriteLine ();
}


---
--
-

for (int i=1;i<=5;i++)
{
for (int j=1;j<= (5-i); j + +)
{
Console.Write ("");
}
for (int t=1;t<= (2*i-1); t++)
{
Console.Write ("#");
}
Console.WriteLine ();
}

C#for statements

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.