C#语言入门详解笔记(6)—P13、P14、P15、P16 表达式,语句详解_1_2_3_4

C#语言入门详解_哔哩哔哩_bilibiliC#语言入门详解搬运,随youtube上进度更新。刘老师的C#视频教程到30集就告一段落了,感谢刘老师在这5年间的付出。能上youtube的同学可以去刘老师的主页观看,https://www.youtube.com/channel/UCmvAggiJGwJGSzAtqFRBQ7g重新上传,修复31P无声音问题https://www.bilibili.com/video/BV1wx411K7rb

目录

1、表达式得定义

1.1、什么是表达式

1.2、C#语言对表达式的定义

2、各类表达式概览

2.1、C#语言中表达式的分类

2.2、复合表达式求值(C#文档找不到)

2.3、参考C#语言定义文档

3、语句的定义

3.1、Wikipedia对语句的定义

 3.2、C#语言对语句的定义

4、语句详解

4.1、声明语句

局部变量声明

4.2、表达式语句

4.3、块语句(简称”块“)

4.4、选择(判断、分支)语句

4.5、迭代(循环)语句

4.6、跳转语句

4.7、try...catch...finally语句

4.8、using语句*

4.9、yield语句*

4.10、checked/unchecked语句*

4.11、lock语句(用于多线程)*

4.12、标签语句*

4.13、空语句*


1、表达式得定义

1.1、什么是表达式

(1)Expressions, together with commands and declarations, are one of the basic components of every programming language. We can say that expressions are the essential component of every language.

(2)An expressions is a syntactic entity whose evaluation either produces a value or fails to terminate, in which case the expression is undefined.

(3)各种编程语言对表达式的实现不尽相同,但大体上都符合这个定义:专门用来求值得语法实体

1.2、C#语言对表达式的定义

(1)An expression is a sequence of one or more operands and zero or more operators that can be evaluated to a single value, object, method, or namespace.

  • single value
        static void Main(string[] args)
        {
            int x ;
            x = 100;//产生一个single value
            ++x;//产生一个single value
            x++;//产生一个single value
        }
  • object
        static void Main(string[] args)
        {
            new Form();//得到一个object
        }
  • method
        static void Main(string[] args)
        {
            Action myAction = new Action(Console.WriteLine);//委托——得到一个方法
        }
  • namespace
        static void Main(string[] args)
        {
            System.Windows.Forms.Form myForm = new Form();
        }

 Expressions can consist of a literal value, a method invocation, an operator and its operands, or a simple name. Simple names can be the name of a variable, type member, method parameter, namespace or type.

  • 字面值参与表达式(literal value
        static void Main(string[] args)
        {
            int x;
            x = 100;            //字面值参与构成表达式
            string name;
            name = "Mr.Ok";     //这也是字面值参与构成表达式
        }
  • 函数调用(method invocation
        static void Main(string[] args)
        {
            double x = Math.Pow(2, 3);//函数调用
        }
  • 操作符和操作数(operator and its operands
        static void Main(string[] args)
        {
            int x = 2 + 3;//2和3是操作数,+是操作符
        }
  • 变量、类型、方法参数、命名空间名(simple name
        static void Main(string[] args)
        {
            Type myType = typeof(Int64);//typeof()是操作符;Int64是类型名字,这里是操作数
        }

(2)算法逻辑的最基本(最小)单元,表达一定的算法意图

比如通过3+4 = 7,表达的算法意图是我想知道3+4的和是多少。

(3)因为操作符有优先级,所以表达式也就有了优先级

2、各类表达式概览

2.1、C#语言中表达式的分类

(1)A value. Every value has an associated type.任何能得到值得运算(回顾操作符和结果类型)

int x = 100;

(y = x)也是有值的,表达式的数据类型就是运算得到的值的数据类型

(2)A variable.Every variable has an associated type.

(3)A namespace.

(4)A type.

(5)A method group.例如:Console.WriteLine, 这是一组方法, 重载决策决定具体调用哪一个

Console.WriteLine("Hello,World");//一个成员访问表达式,一个调用方法表达式

(6)A null literal.

Form myForm = null;

(7)An anonymous function.匿名方法

Action a = delegate(){Console.WriteLine("Hello")};//委托
a{};

(8)A property access.

(9)An event access.

(10)An indexer access.

(11)Nothing.对返回值为void的方法的调用

2.2、复合表达式求值(C#文档找不到)

  • 注意操作符的优先级和同优先级操作符的运算方向

2.3、参考C#语言定义文档

  • 仅作参考,不必深究——毕竟我们是在学习语言、不是去实现这门语言

3、语句的定义

3.1、Wikipedia对语句的定义

(1)In computer programming a statement is the smallest standalone element of an imperative programming language which expresses some action to be carried out. A program written in such a language is formed by a sequence of one or more statements. A statement will have internal components(e.g., expressions).

(2)语句是高级语言的语法——汇编语言和机器语言只有指令(高级语言中的表达式对应低级语言中的指令),语句等价于一个或一组有明显逻辑关联的指令。举例:求圆柱体体积。

 3.2、C#语言对语句的定义

(1)The actions that a program takes are expressed in statements. Common actions include declaring variables, assigning values, calling methods, looping through collections, and branching to one or another block of code, depending on a given condition. The order in which statements are executed in a program is called the flow of control or flow of execution. The flow of control may vary every time that a program is run, depending on how the program reacts to input that it receives at run time.

(2)C#语言的语句除了能让程序员”顺序地“(sequentially)表达算法思想,还能够通过条件判断、跳转和循环等方法控制程序逻辑和走向

(3)简言之就是:陈诉算法思想,控制逻辑走向,完成有意义的动作(action)

(4)C#语言的语句由分号(;)结尾,但由分号结尾的不一定都是语句

using System;//这是指令,不是语句

class Student
{
    public string Name;//这是声明
}

(5)语句一定是出现在方法体里

4、语句详解

statement
    : labeled_statement                       标签语句
    | declaration_statement                  声明
    | embedded_statement                  嵌入式语句
    ;

embedded_statement
    : block
    | empty_statement
    | expression_statement
    | selection_statement
    | iteration_statement
    | jump_statement
    | try_statement
    | checked_statement
    | unchecked_statement
    | lock_statement
    | using_statement
    | yield_statement
    | embedded_statement_unsafe
    ;

4.1、声明语句

declaration_statement                                  局部变量声明
    : local_variable_declaration ';'
    | local_constant_declaration ';'
    ;

局部变量声明

local_variable_declaration                                                    
    : local_variable_type local_variable_declarators
    ;

local_variable_type
    : type
    | 'var'
    ;

local_variable_declarators
    : local_variable_declarator
    | local_variable_declarators ',' local_variable_declarator
    ;

local_variable_declarator
    : identifier
    | identifier '=' local_variable_initializer
    ;

local_variable_initializer
    : expression
    | array_initializer
    | local_variable_initializer_unsafe
    ;

4.2、表达式语句

Expression_statement计算给定表达式的值。 由表达式计算的值(如果有)将被丢弃。

expression_statement
    : statement_expression ';'                             //语句表达式 
    ;

statement_expression
    : invocation_expression                               //调用表达式            Console.WriteLine("I am invocation_expression");
    | null_conditional_invocation_expression   
    | object_creation_expression                      //对象创建表达式     new Form();
    | assignment                                                //赋值语句               int x = 100;
    | post_increment_expression                      //后置自增                x++;
    | post_decrement_expression                     //后置自减                x-;
    | pre_increment_expression                        //前置自增                ++x;
    | pre_decrement_expression                       //前置自减                -x;
    | await_expression                                       //以后讲异步编程的时候再说
    ;

并非所有表达式都允许作为语句。 具体而言,不允许使用只计算值(将被丢弃)的表达式(如 x + y 和 x == 1)作为语句。

执行expression_statement将计算包含的表达式,然后将控制转移到expression_statement的终结点。 如果expression_statement可访问,则可到达expression_statement的终结点。

单一职责原则:一个方法实现一个功能

4.3、块语句(简称”块“)

4.4、选择(判断、分支)语句

4.5、迭代(循环)语句

4.6、跳转语句

4.7、try...catch...finally语句

4.8、using语句*

4.9、yield语句*

4.10、checked/unchecked语句*

4.11、lock语句(用于多线程)*

4.12、标签语句*

4.13、空语句*

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值