For loop. For and then the TAB key automatically generates the following
for (int i = 0/* initial conditions */; I < length/* cycle condition */; i++/* status change */)
{
Loop body, execute code; (break jumps out of the loop body)
}
Given the initial conditions, first determine whether the loop condition is met, if not satisfied, skip for Loop statement, if satisfied, then go into the For Loop statement execution, the code within the For statement after execution, will change according to the state, change the variable, and then determine whether the loop condition is met, and then continue to execute the code inside the For statement , until the variable i does not meet the loop condition terminates the loop, or encounters a break, jumping out of the current for loop.
For can be nested.
①console.write ("Please enter the number of five stars you want to print:");
int a = Convert.ToInt32 (Console.ReadLine ());
for (int i = 1; I <=a; i++)
{
for (int j = 1; J <=i; J + +)
{
Console.Write ("★");
}
Console.Write ("\ n");
}
Console.readkey ();
Ii
Console.Write ("Please enter the number of five stars you want to print:");
int a = Convert.ToInt32 (Console.ReadLine ());
for (int i = 1; i <=a; i++)//increment from 1 to n end
{
for (int j = 1; J <= A-i + 1; j + +)//meet condition, print one ★
{
Console.Write ("★");
}
Console.Write ("\ n"); Next command executed, line break
}
Console.readkey ();
③
Console.Write ("Please enter the number of five stars you want to print:");
int a = Convert.ToInt32 (Console.ReadLine ());
for (int i = 1; i <= A; i++)
{
for (int j = 1; J <a-i+1; J + +)//output spaces first
{
Console.Write ("");
}
for (int t = A; t >a-i; t--)//In the output five-star
{
Console.Write ("★");
}
Console.Write ("\ n"); Line break
}
Console.readkey ();
④
Console.Write ("Please enter the number of five stars you want to print:");
int a = Convert.ToInt32 (Console.ReadLine ());
for (int i = 1; i <= A; i++)
{
for (int j = 1; j < I; J + +)
{
Console.Write ("");
}
for (int t = A; t>i-1; t--)
{
Console.Write ("★");
}
Console.Write ("\ n");
}
Console.readkey ();
⑤ Print Diamond
Console.Write ("Please enter a number:");
int a = Convert.ToInt32 (Console.ReadLine ());
for (int i = 1; I <=a; i++)
{
for (int j = 1; J <=a-i; J + +)
{
Console.Write ("•");
}
for (int t =1; t<=2*i-1; t++)
{
Console.Write ("");
}
Console.Write ("\ n");
}//Top half of the diamond
for (int p = 1; p <a; p++)//diamond lower half
{
for (int q = 1; q <=p; q++)
{
Console.Write ("•");
}
for (int k = 1; k < (A-P); k++)
{
Console.Write ("");
}
Console.Write ("\ n");
}
Console.readkey ();
C # for Loop statement-print pentagram and diamond