First, please refer to the operator overview we mentioned in the previous lesson
Today we mainly explain the following operators:
() parentheses operator
{} combination operator
:: Scope resolution
Throw operator
, the comma operator
() parentheses operator
Examples of Use:
A * (B+c) (b>=a) && (c<=a) (a% (b/c)) *d
Feature Description:
1 parentheses have priority execution privileges. That is, the expression in parentheses is prioritized when the expression is an operation.
2 parentheses can be nested, and inner parentheses have higher precedence when nested.
Attention:
Since () as the parentheses operator, the expression inside cannot be empty or can have multiple statements, so it can be considered as a single-mesh operator
Although commas can be used internally, the entire comma expression is finally given as an expression to the parentheses
Tips for use:
Parentheses are typically used to normalize the operation precedence of an expression, ensuring that the expression is calculated according to the developer's expected priority level.
Students who have read the previous article should have this consensus: the operator itself is not a reliable priority!
And parentheses are the only weapon to solve this unreliable!
{} combination operator
Examples of Use:
void Test () { int a[]={0,1,2,3}; { int a = 0; a++;} }
Feature Description:
1 can be used for function entity definition, complex data type definition
Complex data types include: unions, enumerations, structs, classes, and so on
2 can initialize user array contents
Includes an array of basic data types, arrays of complex data types, and so on
3 sub-scopes can be built
You can redefine variables inside a child scope, or even ignore variables defined by external scopes
For example, I have defined a as an array. But in the child scope, I can define a as an integer again.
In the entire child scope, a is treated as an integer, and array A is not visible in the child scope.
Similarly, the child scope defines the integer A, which is also not visible externally.
Think about the 8th lesson, in the loop statement, for and after {}
It can actually be interpreted as a loop execution of a child scope. So the for () {} is actually and for () statements;
Note: You can have any number of statements because the combination operator can be empty internally. So the number of operators cannot be determined
Tips for use:
During the debugging phase, we need some code to help with debugging. At this point we can put the code in the combined operator. The symbol definition in this way does not affect the external code.
Avoid the introduction of new unreliable factors due to the addition of debug code.
:: Scoped transport analytic operator (C + + only)
Examples of Use:
Std::list<int> lsttmp;std::cin in;:: Global variable name
Feature Description:
1 when referencing a global variable, the symbol can be preceded without entering a domain name. But that doesn't mean that the symbol is monocular, it just implies a global domain.
2 This symbol is actually only useful in the compilation phase, the actual operation will not increase the computational capacity of the computer
Tips for use:
When multiple code modules are developed at the same time, a more common problem is that everyone happens to define the same variable name, complex data name.
If you want to modify the code, it's too much of a hassle, and it's easy to introduce more errors. The use of scopes at this time is a good idea.
About scopes, namespaces, we'll talk about later. Here is a brief explanation, these are designed to solve the problem of the same name.
In this way, I just need to indicate which scope the variable or name comes from, so we can solve the problem of duplicate names. When you develop the code, you don't have to worry about it anymore.
Throw operator (c + + only)
Examples of Use:
if (a==0) throw 0;
Feature Description:
1 The operator will throw an exception. The contents of the exception are covered later, here just to mention
Tips for use:
1 The operator can throw basic variable types, custom variable types, and object
2 The operator must mate with a catch, or the thrown exception will be caught by the system
, the comma operator
Examples of Use:
int a = 0;a = a + 1, a + 2, A + 3;printf ("a= A + 1, a + 2, a + 3%d\n", a); <pre name= "code" class= "CPP" > a = 0;
A = (A + 1, a + 2, a + 3);
printf ("a= (A + 1, a + 2, a + 3)%d\n", a);
Feature Description:
1 when calculating, calculate from left to right. Note The example, why the first output is 1, and the next output is 3? Because the assignment operator has a higher priority!!
2 The comma operator is a binocular operation, and the compiler does not allow a, or, a.
Tips for use:
1 can be used in while:
A = 0;while ((a++), a < 3) {printf ("a=%d\n", a);}
Note that this loop is only executed 2 times, and the output is a=1 a=2
2 used in the initialization of for and change condition processing for each cycle
for (i=0,j=0;i<10;i++,j+=2)
Cond......
Zerglurker's C-language tutorial 010--operator details (ii)