C#語言入門詳解筆記(6)—P13、P14、P15、P16 表示式,語句詳解_1_2_3_4

niangniang怪發表於2021-01-03

沒寫完,住院了,以後補

目錄

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、空語句*

 

 

 

 

 

 

 

 

 

 

 

 

 

 

相關文章