net框架c#程序设计6--流程控制1-分支语句.pptVIP

  • 2
  • 0
  • 约1.47万字
  • 约 45页
  • 2017-09-01 发布于河南
  • 举报

net框架c#程序设计6--流程控制1-分支语句.ppt

net框架c#程序设计6--流程控制1-分支语句

.NET框架C#程序设计 流程控制 第一部分 分支语句 本章简介 用于控制程序语句执行顺序的主要结构 If-else语句 比较操作符及它如何与控制结构合作 如何利用嵌套和分支来解决计算上的重要问题 逻辑操作符的运用 对goto语句的使用 Switch语句 作用域和生存期的概念及其含义 条件操作符 6、1 流程控制简介 6、2 if分支语句 if (balance 1000) balance = balance + (0.1 * balance); //executed only if balance 1000 注意: 作为if语句一部分的布尔表达式必须包含于原括号中 C#允许在代码中包含所谓的空语句。一个空语句,不做任何事,仅有一个分号 如果将空语句错误地放在if语句布尔表达式右边括号的后面,它的存在就舍导致非常隐秘的措误。 Else子句 01: using System; 02: 03: class BankAccount 04: { 05: public static void Main() 06: { 07: const decimal InterestRatePaid = 0.1m; 08: const decimal InterestRateCharged = 0.15m; 09: decimal balance; 10: 11: Console.Write(Please enter account balance: ); 12: balance = Convert.ToDecimal(Console.ReadLine()); 13: if(balance = 0) 14: { 15: Console.WriteLine(Interest paid: {0,13:C} , (balance * InterestRatePaid)); 16: balance = balance + (balance * InterestRatePaid); 17: } 18: else 19: { 20: Console.WriteLine(Interest charged: {0,10:C} , -(balance * InterestRateCharged)); 21: balance = balance + (balance * InterestRateCharged); 22: } 23: Console.WriteLine(New balance: {0,15:C} , balance); 24: } 25: } 缩进样式 样式1: if (Boolean_expression) { statements } 样式2: if (Boolean_expression) { statements } 样式3: if (Boolean_expression) { statements } 可以用一个bool类型变量保存布尔表达式的结果 例如:01: bool isPositive; 02: decimal balance = 100; 03: isPositive = (balance 0); 可以在if语句内使用bool类型变量 例如:04: if (isPositive) 05: WriteLine(The account has a positive balance); 比较操作符可以组合包含数字表达式的操作数 可使用圆括号提高可读性: 10 + 30 5 * 4 (10 + 30) (5 * 4) 当两个不同类型的值用比较操作符进行比较时,若算术二元操作符连接了两个值,编译器严格遵循与隐式转换和类型提升相同的规则。 6、4 嵌套if语句 If(number0) { if(number%3==0) Console.WriteLine(“”); } else Console.

文档评论(0)

1亿VIP精品文档

相关文档