多余不说,直接上代码,其实就是突然想起来,然后对某些东西的一个整理,
其中Console.WriteLine显示信息,Console.ReadLine 就类似个暂停键,按Enter继续
后面有此部分代码运行出的结果
#region 循环及运算符(部分)
#region 三目运算符
int ten = 101;
int ifel = ten < 100 ? ten : 100;
Console.WriteLine("三目运算符:" + ifel);
#endregion
#region Switch 运算
Console.WriteLine("Switch 运算");
int SwitchKey = 100;
switch (SwitchKey)
{
case 10:
Console.WriteLine("It is 10");
break;
case 12:
Console.WriteLine("It is 12");
break;
default:
Console.WriteLine("It is default");
break;
}
#endregion
#region for 运算
for (int ia = 0; ia < 3; ia++)
{
Console.WriteLine("通常的for循环写法,ia={0}", ia);
}
Console.WriteLine("----------------------");
Console.WriteLine("稍微复杂些的for循环写法");
int i;
int j = 10;
for (i = 0, Console.WriteLine("sfs"); i < j; i++, j--, Console.WriteLine("i={0},j={1}", i, j))
{
Console.WriteLine("for循环中,i={0}", i);
}
Console.WriteLine("----------------------");
Console.WriteLine("奇葩的for循环写法");
bool stop = false;
for (; !stop; )
{
stop = true;
Console.WriteLine("can u stop"); //只运行一次
}
Console.WriteLine("----------------------");
//for (; ; ) //可编译成功
//{
// Console.WriteLine("can u stop"); //运行时此处死循环
//}
Console.ReadLine();
#endregion
#region foreach 运算
Console.WriteLine("----------------------");
//IEnumerable IEnumerable
List<int> listInt = new List<int> { 1, 2, 3 };
foreach (var intInList in listInt)
{
Console.WriteLine(intInList);
}
#endregion
#region where 运算
Console.WriteLine("----------------------");
int n = 1;
n = 1;
Console.WriteLine("n is 1");
while (++n < 6)
{
Console.WriteLine("n n is {0}", n);
}
Console.WriteLine();
n = 1;
//n++ n=n+1
Console.WriteLine("n is 1");
while (n++ < 6)
{
Console.WriteLine("n++ n is {0}", n);
}
Console.WriteLine();
n = 1;
while (++n < 6)
{
Console.WriteLine("++n n is {0}", n);
}
#endregion
#region do where 运算
int DoWhile=9;
do
{
Console.WriteLine("do while");
}
while (false);
#endregion
#region Condition
object nullObject = false;
//object nullObject = null;
if (!false || (bool)nullObject) //备注: "||"的前面如成立则不会运行到 "||"后面
{
Console.WriteLine("True Condition");
}
else
Console.WriteLine("False Condition");
if (!false | (bool)nullObject)
{
Console.WriteLine("True Condition");
}
else
Console.WriteLine("False Condition");
Console.ReadLine();
#endregion
#endregion
以上代码运行结果截图如下: