using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ObjectOriented.CBaseGrammar
{
class Circulation //循环
{
#region while循环
static void Main(string[] args)
{
/* 局部变量定义 */
int a = 10;
/* while 循环执行 */
while (a < 20)
{
Console.WriteLine("a 的值: {0}", a);
a++;
}
Console.ReadLine();
}
#endregion
#region for循环
//static void Main(string[] args)
//{
// /* for 循环执行 */
// for (int a = 10; a < 20; a = a + 1)
// {
// Console.WriteLine("a 的值: {0}", a);
// }
// Console.ReadLine();
//}
//C# 中 for 循环的语法格式:
//for (init; condition; increment )
//{
//statement(s);
//}
//下面是 for 循环的控制流:
// init 会首先被执行,且只会执行一次。
//判断 condition条件。如果为真,执行循环主体。如果为假,则不执行循环主体,接着 for 循环的下一条语句。
//反复循环主体,然后增加步值,再然后重新判断条件。在条件变为假时,for 循环终止。
#endregion
#region foreach循环
//static void Main(string[] args)
//{
// int[] fibarray = new int[] { 0, 1, 1, 2, 3, 5, 8, 13 };
// foreach (int element in fibarray)
// {
// System.Console.WriteLine(element);
// }
// System.Console.WriteLine();
// // 类似 foreach 循环
// for (int i = 0; i < fibarray.Length; i++)
// {
// System.Console.WriteLine(fibarray[i]);
// }
// System.Console.WriteLine();
// // 设置集合中元素的计算器
// int count = 0;
// foreach (int element in fibarray)
// {
// count += 1;
// System.Console.WriteLine("Element #{0}: {1}", count, element);
// }
// System.Console.WriteLine("Number of elements in the array: {0}", count);
// Console.ReadLine();
//}
#endregion
#region do...while循环
//static void Main(string[] args)
//{
// /* 局部变量定义 */
// int a = 10;
// /* do 循环执行 */
// do
// {
// Console.WriteLine("a 的值: {0}", a);
// a = a + 1;
// } while (a < 20);
// Console.ReadLine();
//}
//C# 中 do...while 循环的语法:
//do{
// statement(s);
//}while(condition );
#endregion
#region 嵌套循环
//static void Main(string[] args)
//{
// /* 局部变量定义 */
// int i, j;
// for (i = 2; i < 100; i++)
// {
// for (j = 2; j <= (i / j); j++)
// if ((i % j) == 0) break; // 如果找到,则不是质数
// if (j > (i / j))
// Console.WriteLine("{0} 是质数", i);
// }
// Console.ReadLine();
//}
#endregion
}
}