C + + comma operator and comma expression

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.