【C#语言入门】11. 表达式、语句详解(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.
- An expression is a syntactic entity whose evalution either produces a value or fails to terminate, in which case the expression is undefined.
- 各种编程语言对表达式的实现不尽相同,但大体上都符合这个定义。
- 简单来记就是专门用来求值的语法实体。
- C#语言对表达式的定义
- 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.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.
- 算法逻辑的最基本单元是表达式,表达一定的算法意图。
- 因为操作符有优先级,所以表达式也就有了优先级。
int x = 1000;//single value
new Form[];//object
Action myAction = new Action(Console.WriteLine);//mathod,"."是成员操作符,Coinsole和WriteLine是操作数
System.Windows.Forms.Form myForm = new Form();//namespace,同样是"."作为操作符
二、各类表达式概览
- C#语言中表达式的分类
- A value. Every value has an associated type. 任何能得到值的运算
- A variable. Every variable has an associated type.
- A namespace.
- A method group. 例如:Console.WriteLine,这是一组方法,重载决策决定具体调用哪一个
- A null literal.
- An anonymous function.
- A property access.
- An event access.
- An indexer access.
- Nothing. 对返回值为void的方法的调用
- 复合表达式的求值
- 注意操作符的优先级和同优先级操作符的运算方向
三、语句的定义
- Wikipedia对语句的定义
- In computer programming a statement is the smallest standalone element of an imperative programming language which expersses 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.
- 语句是高级语言的语法——汇编语言和机器语言只有指令,语句等价于一个或一组有明显逻辑关联的指令。举例:求圆柱体积。
- C#语言对语句的定义
- 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 inwhich 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.
- C#语言的语句除了能够让程序员“顺序的”表达算法思想,还能通过条件判断、跳转和循环等方法控制程序逻辑的走向。
- 简而言之就是:陈述算法思想,控制逻辑走向,完成有意义的动作。
- C#语言的语句由分号结尾,但由分号结尾的不一定都是语句。
- 语句一定是出现在方法体里的。
using System; //这不是语句
class Student
{
public string Name;//这也不是语句
}
四、语句详解
- statement:
- labeled-statement 标签语句
- declaration-statemnt 声明语句
- embedded-statedment 嵌入式语句
- embedded-statement
- block 块语句
- empty-statement 空语句
- expression-statement 表达式语句
- selection-statement 选择语句
- iteration-statement 迭代语句
- jump-statement 跳转语句
- try-statement try…catch…finally语句
- checked-statement check语句
- unchecked-statement unchecked语句
- lock-statement lock语句
- using-statement using语句
- yield-statement yield语句
五、声明语句
//变量初始化器
int x;
int y = 100;
int n, m;
int[] myArray = {1, 2, 3};
//——————————————————————
//常量初始化器
const int a = 123;
六、表达式语句
不是所有的表达式都可以作为语句的。不允许像x+y或者x==1这样只计算一个值(此值之后会被放弃)的表达式作为语句使用。
Console.WriteLine("nihao");
new Form();
int x;
x = 100;
x++;
--x: