Pig C ++ note BASICS (5) expressions and statements, pig Basics

Source: Internet
Author: User

Pig C ++ note BASICS (5) expressions and statements, pig Basics
Pig C ++ note BASICS (Part 5)

Keywords: expressions, statements

The content in this chapter is relatively simple, and there is basically no difficulty in understanding it. It is a matter of knowledge. I was not prepared to write it first, but now I have to take notes and write it. After all, there are some gains. So, I just put forward some points that are easy to confuse and ignore for future reference.

I. Expressions

(1) Concepts

An expression is composed of one or more operation objects. evaluate an expression to obtain a result. A complex expression can be generated by combining an operation with one or more operation objects.

The operators acting on an object are unary operators such as "&" and. The operators used for two objects are binary operators, such as "+", "-", "=", and "!". =.

In C ++, the expression is either left or right. The left value can be on the left and right of the value assignment statement. When an object is used as the right value, it uses the object value (content). When the object is used as the left value of the epic, it uses the Object Identity (location in memory ). The operators we are familiar with all use the left value.

When the key is decltype, we can determine the type obtained by decltype Based on the left and right values. If the expression evaluates to the left value, decltype gets a reference type. If the expression evaluates to the right value, a pointer type is obtained. For example, int * p; then decltype (* p), * p is the left value, int & reference is obtained, and decltype (& p) is the pointer, int **.

(2) Some operators worth noting

Negative. The unsigned operation returns a copy of an object (after being upgraded) after the object value is negative. Note that the unsigned and BOOL types cannot be computed in this way. The negative value of the unsigned type will be upgraded to the signed type. If there is no overflow, it will be safe. If there is overflow, it will generally be "surrounded", which is similar to the overflow mentioned in Chapter 1. The negative value of the boolean type is increased to the int type. We know that true = 1, and the negative value is-1. If the bool type is not 0, the value is true, and the value 0 is false. Therefore, if the bool type is negative, the value is true.

Modulo operation "%", we specify m % (-n) = m % n, (-m) % n =-(m % n ). Simply put, the modulo operation symbol is determined by the symbol of the previous number. (-M) % (-n) =-(m % n ).

Increment operator (++) and decrease operator (--), that is, add 1 to an object and subtract 1 from an object. These two operations can also be used by iterators that do not support arithmetic operations. Let's take the incremental operator as an example. The Descent operator is similar to the previous version ++ I and later version I ++. The pre-version means to first add 1 to I and then return the result as the left value. The post version returns the copy of the original value of the object as the right value, and then auto-adds the object.

Int I = 0;

Int j = I ++;

Cout <I <"" <j <endl;

J = ++ I;

Cout <I <"" <j <endl;

 

The first j = I ++; is the original copy of the j value assigned to I, and then the I is automatically added. The second ++ I is that I is automatically added before being assigned to j. Generally, we prefer to use ++ instead of I ++.

In addition, it is worth noting that the priority of the ++ operator is higher than that of the dereference operator. Suppose p is a pointer, then * p ++ means * (p ++); Add the value of p to 1, then, the initial test copy of the p value is returned as the result of its evaluation. The result of the solution is that the value is not self-added.

Int a [10];

Int * p =;

For (int I = 0; I <10; ++ I)

A [I] = I;

For (int I = 0; I <10; ++ I)

{

Cout <* p ++ <"";

}

 

Conditional operators (? :), Here are a few interesting examples.

Int grade = 50;

Cout <(grade <60 )? "Fail": "pass ");

Cout <(grade <60 )? "Fail": "pass ";

Cout <grade <60? "Fail": "pass ";

The third cout statement is not compiled, and the first two are correct. Let's take a look at the output.

 

The first output is (grade <60 )? "Fail": "pass" indicates the value of this statement. The second output is 1, which means that the value of grade <60 is true. So when you do not know the priority, the safest way is to enclose the brackets.

Shift Operator (<,>), move by binary, remember to shift left <larger, right> smaller, remove more. We can use the displacement operator to deal with arrays similar to the BOOL type, which is not described here. In addition, we are familiar with the cout <, cin> IO operators of the overloaded version, which will be detailed in the subsequent sections, we need to know whether "cout <" returns a cout. cin is a truth.

Sizeof () operator. Yes, it is an operator. It is not a function. Its operation result depends on the type of function. Run sizeof () on the application type to obtain the space occupied by the referenced object. Execute sizeof () on the pointer to obtain the space occupied by the pointer. We can use the following statement to calculate the number of array elements:

Int a [10] = {0 };

Cout <"sizeof (a):" <sizeof (a) <endl;

Cout <"sizeof (* a):" <sizeof (* a) <endl;

Cout <sizeof (a)/sizeof (* a) <endl;

 

The converted static_cast is displayed. It can eliminate WARNNING when forced type conversion is performed.

Convert a constant to a variable: const_cast;

Implicit type conversion is already mentioned in Chapter 1.

In our programming process, some of the strange operators are mentioned above. If the result of your expression is not the best method you want, try it several times.

Take a break and have a sip of water. If you think these questions may help you a little or help me, you can leave a message to encourage me ~ If you want to take it away, you want to reference my link ~ What.

Ii. Statements

Statements are the basic conditional statements, iterative statements, and so on. They are commonly used. Here, we only need to note some points.

If-else statements, which are familiar to us. The only thing we need to note is that in C ++, else is specified to be paired with the nearest unmatched if statement.

If (a) if (B) else c;

Here, else matches if (B. However, we recommend that you use curly brackets to clearly indicate the cause.

Switch () {case:} statement. The Switch variable must be a constant and can be an integer, char, int, or bool, but cannot be a floating point. In addition, each case corresponds to a break. If the break is not written, the next case will be followed and the condition of the next case will not be met. This should be very familiar. Variables cannot be declared across cases. That is

Case 1: int a; break;

Case 2: a = 3; break;

This is not the case. For the reason, see the scope of the variable.

Jump statement break and continue. Applies to the latest loop statement. Break jumps out of the loop, and continue does not execute the following statements after it is encountered, directly entering the next loop. The following code illustrates the problem:

Int a [10];

For (int I = 0; I <10; ++ I)

{

A [I] = I * 6-i * I;

Cout <a [I] <"";

}

Cout <endl;

For (int I = 0; I <10; ++ I)

{

If (a [I]> 7) break;

Cout <a [I] <"";

}

Cout <endl;

 

For (int I = 0; I <10; I ++)

{

If (a [I]> 7) continue;

Cout <a [I] <"";

}

 

 

The Break jumps out of the loop when it encounters a number greater than 7. Continue does not output a number greater than 7.

In addition, there are goto statements, which are generally not used, which can cause some confusion, but sometimes have unexpected miraculous effects, but you still don't need to use them as much as possible. This is a difficult action.

Finally, an exception statement is thrown. To put it simply, try {A program. If there is a problem, throw an exception.} Then catch the exception and handle it accordingly. It is very difficult to handle program robustness and security, and experience is very important.

If someone thinks it's my motivation to write it down ~ Of course, no one will write it without reading it, because knowledge and growth are their own ~~~

 



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.