流 程 控 制 语 句
-
语句和语句块
在C#语言中,每一个语句都有一个起始点和结束点,并且每个语句并不是独立的。语句是C#程序完成某特定操作的基本单位,能够和其他语句有着某种对应的关系。C#语言常用的语句有如下几种:
空语句:只用一个分号“;”结尾。
声明语句:用来声明变量和常量。
常量:常量的声明是const,例如 const int a; 常量是固定值,程序执行期间不会改变。常量可以是任何基本数据类型,比如整数常量,浮点常量等。常量可以被当作常规的变量,只是他们的值在定义后不能被改变。
表达式语句:由实现特定功能的表达式构成。
流程控制语句:设置应用程序内语句快的执行顺序。 -
选择语句
(1) if语句
using System;
namespace test
{
class Program
{
static void Main(string[] args)
{
int a = 3, b = 4;
if(a > b)
{
Console.WriteLine("a 比 b 大。");
}
else
{
Console.WriteLine("b 比 a 大。");
}
Console.ReadKey();
}
}
}
(2) switch语句
using System;
namespace test
{
class Program
{
static void Main(string[] args)
{
int a = 3, b;
switch(a)
{
case 1:
b = 1;
break;
case 2:
case 3:
b = 3;
break;
case 4:
b = 4;
break;
default:
b = 100;
break;
}
Console.WriteLine("b = {0}", b);
Console.ReadKey();
}
}
}
注意,switch语句中,每个case都要有一个break,不然就会报错,但如果case:后面不接任何语句则可以不用加break,如本例中的case2,此时若a = 2 则会跳过case2,而进入后面的case中。若没有任何一个匹配项,则会运行default的内容。如下所示:
using System;
namespace test
{
class Program
{
static void Main(string[] args)
{
int a = 2, b;
switch(a)
{
case 1:
b = 1;
break;
case 2:
case 3:
b = 3;
break;
case 4:
b = 4;
break;
default:
b = 100;
break;
}
Console.WriteLine("b = {0}", b);
Console.ReadKey();
}
}
}
**这里的case2:后不带任何表达式,则会进入case3:中,然后执行b = 3;break;最后b = 3.
**
3. 循环语句
while (布尔表达式){ 循环体; }
(1)首先计算while后的布尔表达式。
(2)如果表达式的结果是true则执行后面的处理语句,执行完毕后将返回 while语句的开头。
(3)如果表达式的结果是false,则返回while语句的结束点,循环结束。
使用do…while语句
使用do…while语句的语法格式如下:
do
循环体;
while (布尔表达式);
上述do…while语句的执行流程如下:
(1)先转到do后面的处理语句。
(2)当执行来到处理语句的结束点时,计算布尔表达式。
(3)如果表达式的结果是true,则执行将返回到do语句的开始。否则, 将来到do语句的结束点。
for语句
for(初始化表达式;条件表达式;迭代表达式){
处理语句
}
循环语句有for和while,这里用计算100!的例子来了解for和while
using System;
namespace test
{
class Program
{
static void Main(string[] args)
{
int a = 1, sum = 0;
Console.WriteLine("计算100!");
while(a <= 100)
{
sum += a;
a++;
}
Console.WriteLine("100! = {0}", sum);
Console.ReadKey();
}
}
}
using System;
namespace test
{
class Program
{
static void Main(string[] args)
{
int a, sum = 0;
Console.WriteLine("for语句 计算100!");
for(a = 1;a <= 100; a++)
{
sum += a;
}
Console.WriteLine("100! = {0}", sum);
Console.ReadKey();
}
}
}
do while语句则和while语句一样,只是循环体执行的先后问题,while语句是先判断布尔表达式的值,在执行循环体,而do while则是先执行循环体,然后在判断布尔表达式的值。
4. 跳转语句
(1) break
作用是退出当前所在的处理语句,但是只能退出当前层的语句,而不能全部退出
(2) continue
作用是能够忽略循环语句块中位于它后面的代码,从而直接开始另外新的循环
using System;
namespace test
{
class Program
{
static void Main(string[] args)
{
int a = 0;
//注意分别使用continue和break的不同
while(a < 5)
{
a++;
if (a == 3)
{
break;
}
Console.WriteLine(a); // 此时只有1和2打印出来,因为a = 3的时候break语句跳出了循环
}
Console.WriteLine("==================================");
a = 0;
while (a < 5)
{
a++;
if (a == 3)
{
continue;
}
Console.WriteLine(a); // 此时打印出来的有1,2,4,5 没有3,因为a = 3的时候continue语句跳过了a = 3时的语句
}
Console.ReadKey();
}
}
}
实践案例与上机指导
在C#程序中,return语句能够控制返回到使用return语句的函数成员的调用者。在return语句后面,可以紧跟一个可选的表达式,不带任何表达式的return语句只能用在没有返回值的函数成中。
using System;
namespace test
{
class Program
{
static int Add(int x, int y)
{
return x + y;
}
static void Main(string[] args)
{
int result;
result = Add(3, 4);
Console.WriteLine("result = {0}", result);
Console.ReadKey();
}
}
}