[15] Learning C # expressions, statements Comments (03) block statement, the select statement, try statements

Block statement

1. What is a block of statements?

Here Insert Picture Description
The block statement can imagineContainer statements, C #, all the statements (statement of three categories: the label statement, declaration, embedded statement) can be written in a block statement.

2. What is the statement list?

Statement list (statement list), written by one or more sequential statements.
List-of Statement:
of Statement
of Statement of Statement-List

In other words, the statement list is either a statement or a set of statements

3. How to identify block statement?

Here Insert Picture Description

4. points to note about the block statement

(1)Block statement as a statement in any case will be treated CompilerThere was no way that the number of sub-block statement statement
(2) the compiler will block statement is considered complete statement, you do not have curly braces plus semicolon

The scope of variables

Here Insert Picture Description

6. A tips

Sometimes the beginning and end of the program braces braces are not on the same page, too far apart, you can use [ctrl + right curly bracket key] Jump between the start and end braces braces, make the program easier to read.

7. brief description label statements

In front of a label statement is the statement with a label. Tag is an identifier, the same variable identifier naming convention, but without the first data type.

Selection (decision branch) statement

1. What is the select statement?

Selecting a select statement to be executed from a plurality of statements according to a given value of the expression.
of Statement-Selection:
IF-of Statement
Switch-of Statement

选择语句包括 if语句和 switch语句

2.if语句

Here Insert Picture Description
if语句又分为两种情况:带else分支的,不带else分支的;
(1)if ( boolean-expression ) embedded-statement

  • 如果 "boolean-expression"的值为true,执行embedded-statement,如果为false,则什么都不做

  • 对于embedded-statement需要注意的三点
    <1>if ( boolean-expression ) embedded-statement,这是一条语句,在这一条语句中,可以被嵌入一条语句,这条语句就叫做嵌入式语句(嵌入式语句名称的由来)
    <2>既然是嵌入式语句,就不能在此处再写其他类型的语句(标签语句,声明语句)
    <3>在此处只能放一条嵌入式语句,如果要执行的逻辑比较复杂,不要忘了块语句也是嵌入式语句block 用于在只允许使用单个语句的上下文中编写多条语句

(2)if (boolean-expression) embedded-statement else embedded-statement

  • 如果boolean-expression值为true,执行true分支;如果为false,执行false分支(也称else分支)
  • 带有else分支的 if语句覆盖了所有的情况

你做到了吗?:)

  • 编程规范建议:但编写if语句时,无论什么情况,都使用块语句作为if语句的嵌入式语句,哪怕要执行的语句真的只有一条
  • 为了增强代码可读性,需要适当使用缩进;缩进并不会改变代码的逻辑
  • 要善用visual studio 的代码提示(code snippet):【if + 两下tab键】,编译器会自动准备好if语句的格式,往里填充即可

3.如何写出结构清晰的if语句嵌套

由于if语句本身(选择语句)也是嵌入式语句,所以if语句的嵌入式语句就可以写入if语句,从而形成树状分支结构(if语句嵌套),如果写的时候不小心,那么对于复杂的嵌套结构,代码的可读性就会非常差。

  • 逻辑的优化称为代码的重构
  • 如果要验证一个逻辑正确与否,建议多用边界值进行验证

需要解决的实际问题:
编写一段代码,对学生成绩进行等级评定,具体要求

分段 等级
80~100 A
60~79 B
40~59 C
0~39 D

刚开始我会这样实现代码
Here Insert Picture Description

进行逻辑的优化(代码重构)

  • 当true分支或else分支里只有一条语句时,花括号是可以省略的(而且并不会引起逻辑上的改变)
    Here Insert Picture Description

4.switch语句

Here Insert Picture Description

在代码示例前需要注意的几点

  • 先仔细看一遍switch语句的格式
  • 要记得使用代码提示
  • break代表着一个switch-section的结束(每一个switch-section都需要一个break做为结束)
  • case标签后跟着的常量表达式必须与switch关键字后圆括号中的表达式的类型一致
  • 可以有多个标签来对应着一组语句(一旦一个标签后跟了语句,就变成了一个switch-section,而不是一个单独的标签了,此时就必须显式地写一个break来代表switch-section的结束;如果发现多个标签做着同样的事,那就把这几个标签连起来写)
  • default分支可以暂且理解为if语句中的else分支

代码示例:
Here Insert Picture Description

但是改代码还存在着一个非常明显的bug,那就是:如果给score变量赋的值为101~109,得到的结果仍然为 A,而不是期望的 “Input Error!”,因为该范围内的值除以10,对应着case10,需要对代码进行改进
Here Insert Picture Description

对于switch语句需要特别注意的三点:
(1)case标签后跟着的表达式必须是常量表达式
(2)每一个switch-section后必须跟一个break作为结束
(3)switch表达式的类型必须是文档中的那几种(没有任何浮点类型!)

编程规范建议:在写switch语句时,不论什么情况,都把default写出来,以避免一些意想不到的逻辑错误

善用代码提示!(尤其是当switch表达式为枚举类型,代码提示非常强大,非常便捷!)

try语句

1.什么是try语句?

Here Insert Picture Description

  • try语句尝试执行一个语句块,如果在这个语句块中发生异常,try语句就会使用它的catch子句捕捉到异常,然后可对捕捉到的异常分门别类,再进行处理
  • try语句还可带一个finally子句,不论异常是否发生,finally语句都会执行;
  • try语句有三种形式,文档中的第三种形式:对于一个try语句而言,可以有多个catch子句,但只能有一个finally子句,而且,当出现多个catch子句时,只执行其中一个;
  • catch子句有两种:(1)通用catch子句:不论发生什么类型的异常,都能将其捕捉到(2)专用catch子句:只能捕捉到某一种类型的异常

2.如何在程序中使用try语句来捕捉异常,使程序具有容错性?

需要补充的内容:

int 有一个静态方法:Parse,该方法的功能:把一个字符串类型的值解析成整数类型
Parse方法可能会抛出的异常:
如果字符串为空,则抛出ArgumentNullException异常;
如果字符串内容不是数字,则抛出FormatException异常;
如果字符串内容所表示数字超出int类型可表示的范围,则抛出OverflowException异常;

代码示例:
Here Insert Picture Description

使用try语句让方法具有容错性的步骤
(1)先想清楚要try的是什么,也就是说在哪些地方可能会发生异常?
比如说,在上述Add()方法体中的三个语句都有可能发生异常:传入实参的类型不对;传入了null值;两个实参都没有值溢出,但相加结果result却超出范围等
(2)决策:对于这些异常要精确地处理,还是粗略地处理
最粗略的处理:这三个语句全部放在放在一个try语句中,其中任意哪步出问题都会报错
最精确的处理:针对每一条语句,都有其try-catch

现在来修改代码,使该方法具有容错性
Here Insert Picture Description

3.如何把包含在异常中的消息直接传递给用户?

catch ( class-type identifier(opt) ) block
identifier The : Identifier, is actually a variable, citing the examples of class-type in this example is after the catch clause catch anomalies generated
Here Insert Picture Description

4. What usually written in the finally clause?

When the try statement, whether an exception occurs or not, the finally clause will always be executed in the finally clause, usually write two types of content:
(1) release system resource statement
Sometimes the program will apply for access to some resources (such as databases), if not well before the program ended, an exception occurs, the program will jump directly to a particular catch clause, the database connection channel will never be closed to avoid this situation, the finally clause will be written in the statement release system resources
Performing recording (2) procedure (log)
Here Insert Picture Description
Write benefits program execution record:
you can know that "0" is the normal operating procedures results: 0 + 0 = 0;
or procedural defaults occur abnormal obtained, but not the desired result

5.throw keyword

Need to be clear: throw keyword for beginners generally less than

: The process in which only a few exceptions, for the remaining anomalies, throw (throwing) out, and who calls the method, who is going to catch an exception, and processed
Here Insert Picture Description

Note : throw keyword more flexible, if you omit the identifier ane, the compiler knows what it means, and the correct implementation of

When writing code, to develop good habits, once found a statement exception may occur, it is necessary to use a try statement

Published 29 original articles · won praise 3 · Views 947

Guess you like

Origin blog.csdn.net/weixin_44813932/article/details/103844162