微信公众号已开通,搜索微信公众号:程序喵星人。点击关注^_^
3.流程控制语句
控制语句即用来实现对程序流程的 选择、循环、转向 和 返回 等进行控制。
用途:控制语句用于控制程序的流程,以实现程序的各种结构方式。
它们由特定的语句定义符组成。C#语言有九种控制语句。可分成以下三类:
1.条件判断语句
if语句、switch语句;
2.循环执行语句
do while语句、while语句、for语句、foreach语句
3.转向语句
break语句、continue语句、return语句、goto语句(此语句尽量少用,因为这不利结构化程序设计,滥用它会使程序流程吴规律,可读性差)
1.选择结构语句
1.if条件控制语句
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; // if语句 namespace Lesson_10_1 { class Program { static void Main(string[] args) { /* // example_1: 判断是否成年 Console.WriteLine( "请输入年龄:" ); string strAge = Console.ReadLine(); // 接受用户的输入 int intAge = Convert.ToInt32( strAge ); // 选择语句 if ( intAge >= 18 ) { Console.WriteLine("用户已经成年"); } else { Console.WriteLine( "用户未成年" ); } Console.ReadKey(); // 等待用户的输入,避免屏幕一闪而过 */ // example_2: 18岁以下未成年,18~35岁青年,36~60中年,61之后就是老年 Console.WriteLine( "请输入年龄:" ); string strAge = Console.ReadLine(); // 接受用户的输入 int intAge = Convert.ToInt32( strAge ); if ( intAge < 18 ) { Console.WriteLine( "用户未成年" ); } else if ( intAge >= 18 && intAge <= 35 ) { Console.WriteLine( "用户是青年" ); } else if ( intAge >= 36 && intAge <= 60 ) { Console.WriteLine( "用户是中年" ); } else { Console.WriteLine( "用户是老年" ); } Console.ReadKey(); // 等待用户的输入,避免屏幕一闪而过。 } } }
2.switch条件控制语句
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; // switch语句 namespace Lesson_11_1 { class Program { static void Main(string[] args) { /* // example_1:请输入节气,输出对应的节气 Console.WriteLine("请输入节气,1-4"); string strInput = Console.ReadLine(); switch( strInput ) { case "1": Console.WriteLine( "春天" ); break; // 加上break,表示不会在往下进行比较,终止switch case "2": Console.WriteLine( "夏天" ); break; case "3": Console.WriteLine( "秋天" ); break; case "4": Console.WriteLine( "冬天" ); break; default: Console.WriteLine( "请输入正确的节气(1-4)" ); break; } */ // example_2: 请输入月份,输出对应的季节 Console.WriteLine( "请输入月份,1-3表示春天,4-6表示夏天,7-9表示秋天,10-12表示冬天" ); string strInput = Console.ReadLine(); switch( strInput ) { case "1": case "2": case "3": // 满足上面三个条件中的任意一个,即可以进入到改语句中。 Console.WriteLine( "春天" ); break; case "4": case "5": case "6": // 满足上面三个条件中的任意一个,即可以进入到改语句中。 Console.WriteLine( "夏天" ); break; case "7": case "8": case "9": // 满足上面三个条件中的任意一个,即可以进入到改语句中。 Console.WriteLine( "秋天" ); break; case "10": case "11": case "12": // 满足上面三个条件中的任意一个,即可以进入到改语句中。 Console.WriteLine( "冬天" ); break; default: Console.WriteLine( "请输入正确的月份(1-12)" ); break; } Console.ReadKey(); // 避免显示窗口一闪而过 } } }
2.循环结构语句
定义:
循环结构用于对一组命令执行一定的次数或反复执行一组命令,直到指定的条件为真。
1.while循环控制语句
定义:while 循环反复执行指定的语句,直到指定的条件为假。
语法:
while (条件)
{
// 语句
}
案例1:
计算 从1 + 2 + 3 + ... + 100 的和?
案例2:
银行存入 5000 元人民币,年利率为1.0414,计算公式为:
第一年: 5000 * 1.0414;
第二年:5000 * 1.0414 * 1.0414;
......
计算 求经过几年的存款,本息合计能超过10000元?
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; // while循环语句 namespace Lesson_12_1 { class Program { static void Main(string[] args) { /* // example_1: 求 1 + 2 + 3 ... + 100 的和 int i = 1, sum = 0; // while循环,判断 while 后面的逻辑表达式的值,当值为真时,才会执行 while 里面的语句{ }。 while( i <= 100 ) { sum += i; ++i; } Console.WriteLine( sum ); Console.ReadKey(); // 避免一闪而过 */ // example_2: 求本息合计超过10000元 double dblBen = 5000; int intCount = 0; while( dblBen < 10000 ) { dblBen *= 1.0414; Console.WriteLine( dblBen ); ++intCount; } Console.WriteLine( "经过{0}年后,本息合计超过10000元", intCount ); // 使用插入标签 {0},会使用 intCount 的值替换掉 {0}。 Console.ReadKey(); // 避免一闪而过 } } }
2.do...while循环控制语句
do...while 循环与 while 循环类似,二者区别在于 do...while 循环中即时条件为假时,也至少执行一次该循环体中的语句。
语法:
do
{
// 语句
} while(条件)
案例练习--使用 do...while 循环实现
1.计算出从1 + 2 + 3 + 4 ... + n 中何时能到 78 的值。求 n 的值?
2.银行存入5000元人民币,年利率为 1.0414,计算公式为:第一年:5000 * 1.0414;第二年:5000 * 1.0414 * 1.0414;......;计算:求经过几年的存款,本息合计能超过 10000 元?
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; // do while 循环 namespace Lesson_13_1 { class Program { static void Main(string[] args) { /* // example_1: do...while循环 int i = 10; do // 无论如何都会执行一次循环体 { Console.WriteLine(i); } while ( i < 0 ); */ /* // example_2: 计算出从1 + 2 + 3 + 4 ... + n 中何时能到 78 的值。求 n 的值? int intNum = 1, intSum = 0; do { intSum += intNum; ++intNum; } while ( intSum < 78 ); Console.WriteLine( intNum ); */ // example_3: 经过几年的存款,本息合计能超过 10000 元 double dblTotal = 5000; int intYear = 0; do { dblTotal *= 1.0414; ++intYear; } while ( dblTotal < 10000 ); Console.WriteLine( intYear ); } } }
3.死循环
由于编写代码错误或者程序逻辑错误而定义了永远无法终止的循环时,便成了 无限循环,又称 死循环。
例如:
int i = 1;
while( true )
{
++i;
}
// while 条件永远为 true,死循环。
4.for循环控制语句
for 循环要求只有在对特定条件进行判断后才允许执行循环。
这种循环用于将某个语句或语句块重复执行预定次数的情形。
语法:
for (初始值; 条件; 增/减)
{
// 语句
}
案例:
利用for循环求出以下运算的结果:求出:1 + 2 + 3 + ... + n 的值,n为控制台输入的数值。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; // for循环 namespace Lesson14_1 { class Program { static void Main(string[] args) { /* // example_1: 从 1 数到 100 这样的循环 for ( int i = 1; i <= 100; ++i ) { Console.WriteLine( i ); } */ // example_2: 1 + 2 + 3 + ... + n Console.WriteLine( "请输入运算的n的值:" ); int intNum = Convert.ToInt32( Console.ReadLine() ), intSum = 0; for( int i = 1; i <= intNum; ++i ) { intSum += i; } Console.WriteLine( intSum ); /* // example_3: 九九乘法表 for (int i = 1; i < 10; ++i) { for (int j = 1; j <= i; ++j) // 嵌套循环 { Console.Write( "{0}*{1}={2} ", j, i, j * i ); // 为制表符 } Console.WriteLine(); } */ } } }
3.转向语句
C# 提供了 4 个命令,来控制循环的执行。
break -- 立即终止循环。
continue -- 立即终止当前循环,而进行下一次的循环。
goto -- 可以跳出循环,转到已经标记好的代码位置上。
return -- 跳出循环及其所包含的函数体。
案例练习:
1.计算从 1 + 2 + 3 + ... + n >= 78 中 n 的最小值?
作业:
1.求斐波那契数列。其中 n 为控制台随机输入的数字。
a.说明:斐波那契数列(Fibonacci Sequence),又称黄金分割数列,指的是这样一个数列:1,1,2,3,5,8,13,21...
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; // 转向语句 namespace Lesson15_1 { class Program { static void Main(string[] args) { /* // example_1: 从 1 到 100, break 结束整个循环 for ( int i = 1; i <= 100; ++i ) { if ( 50 == i ) { break; // 跳出整个循环 } Console.WriteLine( i ); // 只会输出 1~49 } */ /* // example_2: 从 1 到 100, continue 结束本次循环,执行下一次循环 for ( int i = 1; i <= 100; ++i ) { if ( 50 == i ) { continue; // 结束本次循环,这次循环的后面的语句不会执行 } Console.WriteLine( i ); // 输入的数值,会没有:50 } */ /* // example_3: 求水仙花数。一个三位数abc,满足 a * a * a + b * b * b + c * c * c = abc,就是一个水仙花数 for ( int i = 100; i < 1000; ++i ) { int a = i / 100; // 百位数 int b = i % 100 / 10; // 十位数 int c = i % 10; // 个位数 if ( ( a * a * a + b * b * b + c * c * c ) != i ) { continue; } Console.WriteLine( i ); } */ // example_4: 1~1000的素数 for (int i = 2; i <= 1000; ++i) { bool bSu = true; for (int j = 2; j < i; ++j) { if (i % j == 0) { bSu = false; break; } } if (bSu) { Console.WriteLine(i); } } } } }