Let's take a look at C ++ 3. Expressions and operators, expression Operators
An expression is a combination of numbers, operator numbers, and constraint variables in a meaningful arrangement that can obtain values. The formula we usually call is an expression after removing the number and equal sign.
The readers who have read the second chapter should understand the variables and numbers. Here I want to talk about operator numbers.
In our daily life, operator numbers are similar to +-× operator. In C ++, there are five types of operations that can be directly completed: addition (+) and subtraction (-), multiplication (*), Division (/), and Division (% ).
First of all, multiplication is not represented by X. This is because that symbol is a difficult symbol in the various keyboard coordinates we use, * You only need to move the left hand and the right hand to move up, but it is a little slower than normal typing. However, division is divided into two parts. In fact, this is for integer types. For floating point numbers, there is only one/, because when we perform operations, the data type used in the operation must be highly consistent with the data type of the result. To avoid Decimals in division, we can only divide the data into two parts. 9 then 2 = 4... 1. The process of seeking 4 is the process of division, and the process of seeking 1 is the process of Division. I think this is a good understanding.
In fact, this design provides us with a variety of benefits. For example, when we need to print four rows for each number, we can use the remainder operation.
There is also a condition operator that can help us solve the previous problem.
A = B/c? 1: 2;
This sentence means that if the result of Division B from division c is not 0, then a is equal to 1, otherwise it is equal to 2. Why do we have to compete with zero? This is because the computer uses binary to store things, and every bit of binary can be understood as a switch. 0 means to close, indicating no, 1 means to enable it to indicate yes. In the program, we regard any number not 0 as enable, that is, yes, so the previous conditional expression can be understood as yes.
A = condition? If so: if not
We can write it
Criterion
? If so
: If not
In this way, you will understand more about this.
Let's introduce the logical operators ,! & | ^ These four logical operators represent non, And, or, respectively. If you have never touched the set theory before, the explanation of the three operators may involve a lot of effort, so I will not give them a detailed explanation. I just want to give a brief introduction to the logic circuit (in fact, this is also very troublesome ), there are three operators in logical circuits. First, no (!),
We call the thing in the middle a door. What he represents here is a non-door. This switch is a magic switch, which can supply power to electrical appliances) to a low level (power supply is not allowed for electrical appliances), you can also change the low level to a high level. That is to say, the program converts true to false and false to true. If a is a Boolean variable, it is true ,! A = is equal to false. Of course, do not be misled by my previous remarks. The value of a is not changed, but the value of expression is changed. He can also be used for numbers. He will convert all numbers that are not 0 to 0 and 0 to 1;
This is in connection with the door. It is similar to a series. Only two of them are high levels can output high-level points. In other cases, only low-level electricity is output. He is like a Chinese character. In the expression, if both sides of the Ampersand (&) are true, true is returned. Otherwise, false is returned. In order to save space, I will not continue to map, but use this image to show the remaining two operators. Or the door is in parallel, as long as one is a high level, it will output a high level. If there are two high levels, then it still outputs a high level. If both are low, it can only output a low level. In an expression, if either or (|) is true, true is returned. Otherwise, false is returned. The last is the exclusive OR gate. His request is strange. He requires that the two inputs must be different, that is, either the high level or the other is the low level, so that he can output the high level, otherwise, only the low level is output. In an expression, if either or (^) is true and the other is false, true is returned. Otherwise, false is returned.
& | ^ All values can be used for numerical calculation, but their calculation results are not limited to 0 and 1. They perform bitwise Calculation on two numbers based on the binary system, if this parameter is set to 1, the value 0 indicates false, and the value 0 indicates false. Then, the return value is the binary number (usually in decimal format ).
Another thing is that we call it a composite value assignment operator. If we want a to be a + B (B can be a variable or a constant, we can write it like this
A = a + B; "here, there is something that beginners may not understand, that is, why there is a at both ends of the equal sign. In fact, before assigning a value, first obtain the value of the following expression and then assign it to. Therefore, the in the following expression is equivalent to the value corresponding to the original a pair, while the in the front is only a variable assigned a value, a = a + B and c = a + B are essentially the same. "
We may want to replace a with aaaaaaaaaa. At this time, both sides of the equal sign have aaaaaaaaaa, which is very difficult to write. How can we save this part of work?
a += b;
Similarly, we also have a series of operators-= * =/= % =.
Next, we are so lazy that we don't want to write anything to B. What should we do. When B is 1, we can indeed save B. We need to write a ++ or ++ a in this way. In the same series, there are also a -- and --, but there is no multiplication or division, and the reason is self-thinking.
Why are there two methods of writing: prefix and Postfix? In fact, if they are only used to change the value of a, they are the same. They are different, is the value they return.
The expression returns values. 1 + 2 returns 3, 5/2 returns 2, just like the preceding conditional operator. Based on the criterion, it determines whether to return, 1 or 2, even if this expression is used to assign values, it also returns values. a ++ and ++ a certainly cannot be an exception. The a of a ++ is in front, and the returned value is a when there is no + 1, that is, a is returned first, and then a + 1 is returned; the return value of a of ++ a is a of + 1, that is, a + 1 is returned first. Is it difficult? Write a program by yourself, such as this program.
1 #include<iostream> 2 using namespace std; 3 4 int main() { 5 int a = 10; 6 cout << a++ << ' ' << a << "\n"; 7 a = 10; 8 cout << ++a << ' ' << a << "\n"; 9 return 0;10 }
The next cute thing is the Relational operators, which include
= Equal
! = Not equal
<Less
> Greater
> = Greater than or equal
<= Less than or equal
Their return values are usually true or false, 2 = 3 returns false, 6/2 = 3 returns true. Here, we need to pay attention to two points. The first is equal to = rather than =, this is because = is considered to be the operator of the value assignment expression. To avoid conflicts, we use = to represent equals.
The second point is the last content in this section.
Let's look at the formula 6/2 = 3. We think it calculates 6/2 first and then X = 3. The answer is true. If it is calculated 2 = 3 first, false is returned, that is, 0, 6/0, and then the program reports an error.
How to judge the order?
If I write a sub-statement like this for you, 6 bytes 3 = 2, you must say this sub-statement is correct, because you will automatically calculate the previous one. In elementary school, we were told that X-Dragon is better than +-first, and then we know that the request is better than X-dragon. The rules we are told are actually incomplete, because there are still many operations that are not included in these five (or six) operations.
In the program, we give different operations a certain priority. The higher the priority, the earlier the calculation. Next, I will put the introduced operator priority below, and the priority will decrease in sequence from top to bottom.
Logical operator!
Arithmetic Operator */%
Arithmetic Operators +-
Relational operators <> <=> =
Relational operator =! =
Logical operators &&
Logical operator |