C#高级编程(第七版)读书笔记(2)——语句流
语句流:
1.if,else
if (condition)
{
statement(s)
}
else
{
statement(s)
}
还可以在里面嵌套else if语句,if可以单独使用。
2.switch
switch (Console.ReadLine())
{
case "a":
case "b":
case "c":
case "d":
Console.WriteLine("hello");
break;
case "f":
goto case "d";
break;
default :
break;
}
注意,每条case必须用break跳出。如果不写并且该语句不能有表达式,则会执行下面的语句。可以用goto跳到其他case中。
3.循环
(1)for
for (initializer;condition;iterator)
{
statement(s)
}
(2)while
while(condition)
{
statement(s)
}
(3)do...while
do
{
statement(s)
}while(condition)
(4)foreach
foreach(int temp in arrayOfInts)
{
Console.WriteLine(temp);
}
temp的值不能在迭代语句中改变,需要改变该值应该用for循环。
4.goto
goto Label1;
statement(S)
Label1:
statement(S)
不能跳转到for循环代码块中,不能跳出类的范围,不能退出try ...catch块后面的finally块。
5.break
用于跳出swith,for,foreach,while,do...while循环
6.continue
类似于break,用于swith,for,foreach,while,do...while循环,退出当前循环的迭代,进行下一次迭代。而不是退出循环。
7.return
用于退出类的方法,把控制权返回方法的调用者。如果有返回类型的值,则返回,如果void,则无返回值。