1. C program statements
1. C program statements
The execution part of a C program is composed of statements. The functionality of the program is also implemented by executing statements. C statements can be divided into the following five categories:
1.1. 1. Expression statement
The expression statement consists of an expression followed by a semicolon ';'. Its general form is: expression; Executing an expression statement is to calculate the value of the expression. For example: x=y+z; Assignment statement y+z; Addition statement, but the calculation result cannot be retained and has no practical significance i++; Add 1 statement to increase the value of i by 1.
1.2. 2. Control statement
Control statements are used to control the flow of a program, in order to implement various structural ways of the program.
They are composed of specific statement qualifiers. There are nine control statements in C language. It can be divided into the following three categories:
(1)Conditional statements, if statements, switch statements
(2)Loop execution statement, do while statement, while statement, for statement
(3)Turn statement, break statement, goto statement, continue statement, return statement
1.3. 3. Empty statement
A statement consisting only of semicolons'; 'is called an empty statement. An empty statement is a statement that does not execute anything. Hollow statements in a program can be used as empty loop bodies. For example, while (getchar()!=') \n'); The function of this statement is to re-enter as long as the character entered from the keyboard is not carriage return. The loop body here is an empty statement.
1.4. 4. Assignment statement
An assignment statement is an expression statement composed of an assignment expression and a semicolon. Its general form is: variable=expression; The function and characteristics of an assignment statement are the same as those of an assignment expression. It is one of the most commonly used statements in programs. In the use of assignment statements, the following points should be noted:
- Since the expression to the right of the assignment symbol "=" can also be another assignment expression, the following formal variable=(variable=expression); It is established, thus forming a nested situation. The general form after expansion is: variable=variable=...=expression; For example:
a=b=c=d=e=5;
According to the right-hand conjunction of the assignment operator, it is actually equivalent to:
e=5;d=e;c=d;b=c;a=b;
Pay attention to the difference between assigning initial values to variables and assigning statements in variable descriptions. Assigning initial values to variables is a part of variable description, and the initialized variable must still be separated by commas from other similar variables that follow it, while assignment statements must end with semicolons.
It is not allowed to assign initial values to multiple variables consecutively in the variable description. The following statement is incorrect:
int a=b=c=5;
or:
int a=5,b=5,c=5;
And assignment statements allow for continuous assignment.。
- Pay attention to the difference between assignment expressions and assignment statements. An assignment expression is an expression that can appear anywhere an expression is allowed to appear, while an assignment statement cannot.
The following statement is legal:
if((x=y+5)>0) z=x;
The function of the statement is that if the expression x=y+5 is greater than 0, then z=x. The following statement is illegal:
if ((x=y+5;)>0) z=x;
Because x=y+5; It is a statement and cannot appear in an expression.