C#语法基础
2021-11-30 本文已影响0人
秦海波
分支语句
if
int i = 10;
if(i == 10)
{
Console.WriteLine("i等于10");
}
if-else
static void Main(string[] args)
{
int i = 10;
if(i == 20)
{
Console.WriteLine();
}
else
{
Console.WriteLine("i不等于20");
}
if (i < 10)
{
Console.WriteLine("i小于10");
}
else if (i > 10)
{
Console.WriteLine("1>10");
}
else
{
Console.WriteLine("i=10");
}
}
switch
int i = 3;
switch (i) {
case 0:
Console.WriteLine("男的");
break;
case 1:
Console.WriteLine("女的");
break ;
case 2:
Console.WriteLine("不男不女的");
break;
default:
Console.WriteLine("我可不知道");
break;
}
循环
for
int i = 10;
for (int j = 0; j < i; j++)
{
Console.WriteLine(j);
}
增强for
string[] strings = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };
foreach (string s in strings)
{
Console.WriteLine(s);
}
while
int i=0;
while (i < 10)
{
i++;
Console.WriteLine(i);
}
#do-while
int i=0;
do
{
Console.WriteLine(i++);
}while (i<10);