In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Xiaobian would like to share with you the example analysis of operators and expressions in C++. I believe most people don't know much about it, so share this article for your reference. I hope you will learn a lot after reading this article. Let's take a look at it!
In a programming language, an "expression" can be similar to a "formula", that is, a certain calculation is carried out according to one's own expectations, and the expression is composed of operations consistent with operands, and so on. C++ contains unary (unary) operators, binocular (binary) operators and ternary (ternary) operators, which correspondingly constitute a variety of expressions. Operators have priority and associativity, priority refers to the order of calculation, and associativity refers to whether the sibling operator is calculated from left to right or right to left.
(1) arithmetic expression
An arithmetic expression consists of arithmetic operators, operands, and parentheses. The basic operators include: add +, subtract -, multiply *, divide\, and take the remainder%. Only "addition and subtraction" are unary operators when doing "positive and negative" symbols. The rest of the cases are binary operators. C++ has self-addition and self-subtraction-- two unary operators (python should not have self-addition and self-subtraction, because the values in python are immutable, so changing the value will apply for new memory). When there is a self-addition or self-subtraction operation in the expression, be sure to pay attention to whether the self-addition and self-subtraction are before or after the operands, which means add and then evaluate the whole expression before the operands. After the Operand, it means that the whole expression is evaluated first and then added.
For example, the following code:
Int age_1 = 22 int age_2 = 22 int new_age_1;int new_age_2;new_age_1 = age_1++;new_age_2 = + + age_2
Line 5 calculates new_age_ 1, first calculates the whole expression, and then adds age_1 itself, so new_age_1
Equal to 22; when line 6 calculates new_age_ 2, the age_2 is first self-added, and then the expression is evaluated, so new_age_2 is equal to 23. When writing your own code, it's best not to dig holes for yourself and to use less complex expressions.
(2) assignment operator
The assignment operator "=" is a binocular operator, associative from right to left, allowing continuous assignments:
Int x, y, z * * x = y = z = 1
In addition, C++ has + =, * =, / = and other operators, all of which are associative from right to left.
(3) comma operator
The comma operator can separate two expressions, evaluating the expression on the left and then the expression on the right, but because the comma operator takes precedence over the assignment operator, you must enclose the comma expression when assigning a value with a comma operator
The following two lines of code get different x values:
X = (2,3); x = 2,3
The first line x equals 3 and the second line x equals 2. Because the comma operator takes precedence over the assignment operator, you must be careful when using it.
(4) Relational operator
Relational operators are the basis of logical expressions. C++ contains relational operators with two priorities, with the higher priority being greater than, less than, greater than or equal to, and less than or equal to, respectively, represented by =; the lower priority is equal to, not equal to, respectively, with = =,! = represents. The value returned by the logical expression is of Boolean type, True (0) if it is true, and False (1) if it is false.
As follows:
Int x = 2nint y = 3polibool result;result = (x > = y)
C++ also has three logical operators: and, OR, and not, using | |, & &,! Three symbols represent.
(5) Ternary expression
As far as I know, C++ 's only ternary operator is the conditional operator, and its syntax is as follows:
Conditional judgment? Expression 1: expression 2
When the judgment condition is true, expression 1 is executed; when the judgment condition is not true, expression 2 is executed
The code is as follows:
Int x = 2: int y = 3: int result;result = (x > y? 10: 1000)
Because x > y is not valid, the execution expression 2, that is, result, is assigned a value of 1000.
(6) sizeof function
The book calls sizeof a separate operator, which is treated here as a function for ease of understanding. Sizeof can calculate the number of bytes occupied by an object. The following example calculates the number of bytes occupied by the int type:
Int result=1000;cout
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.