The so-called expression, that is, by numbers, operational symbols, constraints, etc. to be able to obtain the value of a meaningful arrangement of the formation of a combination, we usually say the formula, minus the number and equal sign is an expression.
variables, numbers and so on, read the second chapter of the reader should all understand, I would like to speak here is the operational symbols.
In our daily life, the operation symbol is nothing more than the +-x÷ class, in C + +, can be done directly in five kinds of operations, namely addition (+), subtraction (-), multiplication (*), divide (/) and the remainder (%).
First, the multiplication is not represented by X, because the symbol is a very difficult symbol in all the various keyboard ligands we use, and * just the left hand shift and the right hand move, just a little slower than you normally would. But division is divided into two, in fact, this is for the integral type, for the floating point, then there is only one/, because we are in the operation, in general, the data type used in the operation and the result of the data type to maintain a high degree of consistency, in order to avoid the division of decimals, we can only be divided into two parts. 9÷2 = 4 ... 1, the process of seeking 4 is the process of division, the process of seeking 1 is to go to the remainder of the process, I think this is very good understanding.
In fact, this design provides us with a variety of benefits, such as when we need to make some number of each print 4 times the line, we can use to ask Yu Yun forget.
There is another thing called the conditional operator, he can also try to help us solve the last problem.
A = b/c? 1:2;
The meaning of this sentence is that if B is divisible by the result of C is not 0, then a is equal to 1, otherwise it is equal to 2. Why do we have to compete with 0 that, this is because the computer uses binary to store things, and the binary each bit can be understood as a switch, 0 is closed, indicating no, 1 is the open expression is, in the program, we put any number not 0 is considered to be open, that is, So the last conditional expression is understood to be
A = determining condition? If it is set up, then: if not, then
We can write it down.
Judging conditions
? If established, then
: If not, then
So this thing will understand more.
Introduce the logical operators again! && | | ^ These four logical operators, respectively, represent the non-, and, or, if you have not been exposed to the set theory, the explanation of the three operators may be a bit of a contribution, so I do not give them a detailed explanation, I just briefly talk about the logic circuit (in fact, it is very cumbersome), with the logic circuit to say three operators. First right and wrong (!),
We call the middle thing the door, what he represents here is the non-gate, this switch is a magical switch, he can be high (can be used to power the electrical appliance) to low level (can not be used to power the appliance), can also turn the low level to high level, that is, the corresponding to the program, He will turn true to false and false to true. If A is a Boolean variable, he equals True,!a= is equal to false. Of course, do not be misled by my previous remarks, the value of a does not change, the value of the expression is changed. He could also use numbers, and he would turn everything not 0 into 0, turning 0 into 1;
This is with the door, he is similar to concatenation, must be two high level he will output high-level points, all other cases will only output low-level power. He is like Chinese and, in an expression, if both sides of (&&) are true, he returns true otherwise false. To save space, I'm not going to continue with the stickers, but I'm using this diagram to figure out the remaining two operators. Or the door is in parallel, as long as there is a high level he will output high level, if there are two high, then he still output high, if all is low, he can only output low level. In an expression, if the or (| | | If either side has one or two is true, he will return true, otherwise false. The end is the XOR gate, his request is very strange, he asked two input current must be different, that is, must be a high level and the other is low, he will output high level, otherwise only output low level. In an expression, if XOR (^) is true on either side, and the other is false, he returns true, otherwise false.
&& | | ^ can be used for numerical calculations, but they calculate the result is not limited to 0 and 1, they will be on the basis of 2, the two digits of the calculation, which is 1 for true,0 on behalf of false, he returned is calculated after the binary corresponding to the number (usually decimal).
There is another thing that we call a compound assignment operator, if we want a value of a equal to A+b (b can be a variable, or it can be a constant or constant), we can write
A = a + B; "Here, there is something a beginner might not understand, which is why there is a at both ends of the equal sign, but before we assign it, we need to find the value of the following expression before assigning it to a." Therefore, a in the subsequent expression is equivalent to the original a-pair "value, and the previous a, just an assigned variable, a = a + b and C = a + B is essentially the same." "
We may as well change A to aaaaaaaaaa, at this time, the equal sign both sides have aaaaaaaaaa, writing is very laborious ah, there is no. So, how do we save this part of the job, and we can write that?
A + = B;
In the same vein, we also have the-= *=/=%= series of operators.
Next, we are lazy to even b do not want to write, then what to do. When B is 1, we can really dispense with B, we have to write a++, or ++a, with a series of a--and--a, but not multiply and divide, for reasons that I want.
Why there are two types of pre-and post-writing, in fact, two ways, if only to change the value of a, then they are the same, they are not the same place, in the value they return.
The expression has a return value, 1 + 2 returns 3, 5/2 returns 2, just like the previous conditional operator, he determines whether to return, 1 or 2, even if the expression is used to assign a value, a++ and ++a are certainly not exceptions. a++ A in front, his return value is not + 1 o'clock of a, that is, return a, and then put A+1, and ++a A in the back, his return value is +1 after a, that is, the first a+1, and then return a. Is it very round? Write a program yourself to try it, such as this program.
1#include <iostream>2 using namespacestd;3 4 intMain () {5 intA =Ten;6cout << a++ <<' '<< a <<"\ n";7A =Ten;8cout << ++a <<' '<< a <<"\ n";9 return 0;Ten}
The next cute thing is relational operators, they include
= = equals
! = does not equal
< less than
> Greater than
>= greater than or equal to
<= less than or equal to
Their return value is usually true or false, 2 = = 3 returns false, 6/2 = = 3 Returns True, there are two points to note here, the first is equal to = = instead of =, this is done because = is the operator we think is an assignment expression, in order not to create a conflict, we use = = to indicate equals.
The 2nd is the last part of this verse.
Look at 6/2 = = 3 This formula, we think it is calculated first 6/2 then calculate X = = 3, the answer is true. If he calculates 2 = = 3 First, returns false that is 0, 6/0 then the program will error.
How is the order judged?
If I write you a formula like this, 6÷3 = 2, you must say that this is true, because you will automatically calculate the front. In primary school, we were told that X÷ than +-first count, and then know that the seeking side than the X÷ first. The law we are told is actually not complete, because there are many and many operations that are not in these 5 (or 6) operations.
In the program, we give a certain priority to different operations, the higher the priority is calculated the sooner. Next, I'll prioritize the operators that I've introduced, and put them below, from top to bottom, descending in order of precedence.
logical operators!
Arithmetic operator */%
Arithmetic operator +-
Relational operators < > <= >=
Relational operator = =! =
logical Operators &&
Logical Operators | |
Let's take a look at C + + three. Expressions and operators