if(条件表达式1)
{
语句序列1;
}
[ else if(条件表达式2)
{
语句序列2;
}
…
else{
语句序列n;
}
]
eg: static void Main(string[] args)
{
int score = int.Parse(Console.ReadLine());
if (score >= 90)
Console.WriteLine("优秀");
else if(score>=80)
Console.WriteLine("良");
else if(score>=70)
Console.WriteLine("中等");
else if (score >= 60)
Console.WriteLine("及格");
else
Console.WriteLine("不及格");
Console.ReadLine();
}
switch(表达式){
case 值1:
语句序列1;
case 值2:
语句序列2;
…
case 值n:
语句序列n;
[default:
语句序列n+1;
]
}
eg:
static void Main(string[] args)
{
int score = int.Parse(Console.ReadLine());
switch (score / 10)
{
case 10:
case 9:
Console.WriteLine("优秀");break;
case 8:
Console.WriteLine("良"); break;
case 7:
Console.WriteLine("中等"); break;
case 6:
Console.WriteLine("及格");break;
default:
Console.WriteLine("不及格"); break;
}
Console.ReadLine();
}
while(条件表达式){
循环体;
}
do{
循环体;
}while(条件表达式);
static void Main(string[] args)
{
int n = int.Parse(Console.ReadLine());
int s1 = 0, s2 = 0;
int n1 = 1, n2 =1;
while (n1 <= n)
{
s1 = s1 + n1;
n1++;
}
do
{
s2 = s2 + n2;
n2++;
} while (n2 <= n);
Console.WriteLine("{0} {1} {2} {3}",n1,s1,n2,s2);
Console.ReadLine();
}
for(变量赋初值;条件表达式判断条件;变量增量){
循环体;
}
eg:
for(int x=0①;x<=10②;x++④){
s=s+x;③
}
①-->②-->③-->④
<------------
eg:
int x=0;
for(;x<=10②;x++④){
s=s+x;③
}
for(;;){
…
}
static void Main(string[] args)
{
int n = 100;
int x = 1,s=0;
for (; ; )
{
s += x;//s=s+x;
x++;
if (x > n) break;
}
Console.WriteLine("{0} {1}",x,s);
Console.ReadLine();
}
static void Main(string[] args)
{
int[] x = { 1, 2, 3, 4, 5 };
foreach(int a in x)
Console.Write(a + " ");
Console.ReadLine();
}
本章练习题下载地址:点此下载