This series of articles is a summary of the key words "high quality programming guide-C ++/C language ".
This article summarizes the expressions and basic statements:
Operator priority:
Priority
Operator
Name or meaning
Usage
Integration direction
Description
1
[]
Array subscript
Array name [constant expression]
Left to right
()
Parentheses
(Expression)/function name (parameter table)
.
Member selection (object)
Object. member name
->
Member selection (pointer)
Object Pointer-> member name
2
-
Negative Operator
-Expression
Right to left
Single Object Operator
(Type)
Forced type conversion
(Data Type) Expression
++
Auto-increment operator
++ Variable name/variable name ++
Single Object Operator
--
Auto-subtraction Operator
-- Variable name/variable name --
Single Object Operator
*
Value Operator
* Pointer variable
Single Object Operator
&
Bitwise operators
& Variable name
Single Object Operator
!
Logical non-Operator
! Expression
Single Object Operator
~
Bitwise Inverse Operator
~ Expression
Single Object Operator
Sizeof
Length Operator
Sizeof (expression)
3
/
Division
Expressions/Expressions
Left to right
Binary Operators
*
Multiplication
Expression * Expression
Binary Operators
%
Remainder (Modulo)
Integer expression/Integer expression
Binary Operators
4
+
Add
Expression + expression
Left to right
Binary Operators
-
Subtraction
Expression-expression
Binary Operators
5
<
Move left
Variable <expression
Left to right
Binary Operators
>
Right Shift
Variable> Expression
Binary Operators
6
>
Greater
Expression> Expression
Left to right
Binary Operators
> =
Greater than or equal
Expression> = expression
Binary Operators
<
Less
Expression <expression
Binary Operators
<=
Less than or equal
Expression <= expression
Binary Operators
7
=
Equal
Expression = expression
Left to right
Binary Operators
! =
Not equal
Expression! = Expression
Binary Operators
8
&
Bitwise AND
Expressions & Expressions
Left to right
Binary Operators
9
^
Bitwise OR
Expression ^ expression
Left to right
Binary Operators
10
|
By bit or
Expression | expression
Left to right
Binary Operators
11
&&
Logic and
Expressions & Expressions
Left to right
Binary Operators
12
|
Logic or
Expression | expression
Left to right
Binary Operators
13
? :
Conditional Operators
Expression 1? Expression 2: expression 3
Right to left
Three-object Operator
14
=
Value assignment operator
Variable = expression
Right to left
/=
Assignment After Division
Variable/= expression
* =
Assign value after Multiplication
Variable * = expression
% =
Assign value after modulo operation
Variable % = expression
+ =
Add and assign values
Variable + = expression
-=
Value after subtraction
Variable-= expression
<=
Value after left shift
Variable <= expression
>>=
Value after right shift
Variable> = expression
& =
Bitwise AND postvalue
Variable & = expression
^ =
Value Based on bitwise XOR
Variable ^ = expression
| =
Value by bit or after
Variable | = expression
15
,
Comma Operator
Expressions, expressions ,...
Left to right
Sequential operation from left to right
Source of the operator priority table
Operators are not hard to remember. Priority and associativity are hard to remember!
Therefore, we recommend that you use parentheses to determine the Operation Sequence of expressions if there are many operators in the code line. You can avoid using the default priority, especially when you forget the priority.
If statement (compared with zero value)
Comparison between Boolean variables and zero values:
Boolean variables cannot be directly compared with TRUE, FALSE, or 1 or 0.
The correct method is as follows:
[Cpp]
If (flag) // indicates that the flag is true.
If (! Flag) // indicates that the flag is false.
// The following is a bad style
If (flag = TRUE)
If (flag = FALSE)
If (flag = 1)
If (flag = 0)
Comparison between Integer Variables and zero values:
Integer variables should be replaced with "=" or "! = "Direct comparison with 0
The correct method is as follows:
[Cpp]
If (value = 0)
If (value! = 0)
// The following write method can be misunderstood as a Boolean variable.
If (value)
If (! Value)
Comparison between floating point variables and zero values:
You cannot use "=" or "! = "Compare with any number
Because the floating point type has precision restrictions, you should try to convert it to the "> =" or "<=" Form
Therefore
[Cpp]
If (x = 0.0)
Convert
[Cpp]
If (x> =-EPSINON) & (x <= EPSINON) // Where EPSINON is the allowable error (accuracy)
Comparison between pointer variables and zero values:
Use "=" or "! = "Comparison with NULL
Although the value of NULL is the same as that of 0, they have different meanings.
[Cpp] view plaincopyprint?
If (p = NULL)
If (p! = NULL)
// The following statement is confusing: integer variable
If (p = 0)
If (p! = 0)
// The following statement is a Boolean variable.
If (p)
If (! P)
Sometimes you may see the if (NULL = p) Strange format. This is not a program error, but a programmer wants to prevent the if (p = NULL) if (p = NULL ). The compiler considers if (p = NULL) as legal, but it indicates that if (NULL = p) is incorrect because it cannot be assigned a value.
Efficiency of loop statements
Suggestion:
In multiple loops, if possible, we should try our best to put the longest loop in the innermost layer and the shortest in the outermost layer. To reduce the number of times the CPU cross-switch cycle layer
If logical judgment exists in the loop body and the number of cycles is large, it is recommended to move the logical judgment outside the loop body.
You cannot modify the loop variable in the for loop to prevent the for loop from being out of control.
We recommend that you use the "half-open and half-closed" Method for the value of the for statement's cyclic control variable.
Switch statement
Break must be added at the end of each case statement unless multiple branches are intentionally overlapped.
Do not forget the last default Branch, which must be retained even if the program does not need it. This is to prevent others from mistakenly thinking that you forgot to use default to process www.2cto.com.
Goto statement
Although goto will undermine the structural design style. It may skip the construction of some objects, initialization of variables, and important computing statements, resulting in errors or risks. Therefore, goto is generally not recommended. However, one of its functions cannot be ignored: It can jump from multiple loops to the outside of the loop at once without multiple break statements.