C + + assigns an assignment expression as one of the expressions, so that the assignment can appear not only in the assignment statement, but also in other statements (such as output statements, loop statements, and so on) in the form of an expression. This is a manifestation of the flexibility of the C + + language.
Note that when you use the cout statement to output the value of an assignment expression, enclose the assignment expression in parentheses, if written as "cout<<a=b;" A compilation error will occur.
C + + provides a special operator-the comma operator. Use it to connect two expressions together. Such as
3+5, 6+8
Called a comma expression, also known as the order evaluation operator. The general form of a comma expression is:
Expression 1, Expression 2
The solution to the comma expression is to solve the expression 1 first and then solve the expression 2. The value of the entire comma expression is the value of expression 2. For example, a comma-expression
A=3*5, A*4
The assignment operator has a higher precedence than the comma operator, so a=3*5 should be solved first (that is, "a=3*5" as an expression). After calculation and assignment, the value of a is 15, and then the a*4 is solved by 60. The value of the entire comma expression is 60.
A comma-expression can also form a new comma expression with another expression, such as
(A=3*5, a*4), a+5
The general form of a comma expression can be extended to:
Expression 1, expression 2, expression 3, ..., Expression n
Its value is the value of the expression N.
The comma operator is the lowest level in all operators. Therefore, the following two expressions function differently:
X= (A=3, 6*3)
X=a=3, 6*a
In fact, a comma expression is nothing more than "concatenating" several expressions together. In many cases, the purpose of using a comma expression is simply to get the values of each expression separately, rather than necessarily getting and using the entire comma-expression value, the comma expression is most commonly used in a loop statement (for statement).
When you use cout to output a value of a comma expression, enclose the comma expression in parentheses, such as:
cout<< (3*5, 43-6*5, 67/3) <<endl;
C and C + + language expression Ability is strong, one of the important aspect is that its expression type is rich, the operator function is strong, thus the use is flexible, strong adaptability.
C + + comma operator and comma expression