In the C # programming language, the For loop is almost identical to the for loop inside Java . We've got a few examples here to let you know about The use of the For loop.
Example 1: Calculates The and of 1 to.
Console.WriteLine("The beginning of the calculation of 1 to the and:" );
intresult;
result=0;
for(inti=1; i<= -; i++)
{
Result=result+i;
}
Console.WriteLine("1 to " and for:{0} " , result);
Example 2: Calculates the factorial of a number. For example: the factorial of 6 is:1*2*3*4*5*6.
Console.Write(" Please enter a number:" );
intnum;
num=Convert.ToInt32(Console.ReadLine());
Console.WriteLine(" start calculating factorial:" );
intresult;
result=1;
for(inti=1; i<=num;i++)
{
Result=result*i;
}
Console.WriteLine("The factorial of {0} evaluates to:{1}" , Num,result);
Example 3: 99 multiplication formula.
// outer loop represents the line
for(inti=1; i<=9; i++)
{
// The inner loop represents the column
for(intj=1; j<=i;j++)
{
// Output multiplication formula, is: Column * row = value
Console.Write("{0}*{1}={2}", j,i,i*j);
Console.Write(" ");
}
Console.Write("\ n");
}
This article is from the "Smile" blog, please be sure to keep this source http://yiyiweixiao.blog.51cto.com/2476874/1970312
15. Use of the For loop structure inside C #