Thinking logic of computer programs (3), thinking Logic

Source: Internet
Author: User

Thinking logic of computer programs (3), thinking Logic

Operation

The first section describes how to define data through variables. The last section describes how to assign values to data. With an initial value, you can perform operations on the data. A computer is called a "computing" machine because it is invented for computation. There are different types of operations, and different data types support different operations. This article introduces the main operations of basic data types in Java.

  • Arithmetic Operation: It mainly refers to daily addition, subtraction, multiplication, division
  • Comparison calculation: mainly the daily size comparison
  • Logical operation: operations on boolean values

Arithmetic Operations

Arithmetic Operators include addition, subtraction, multiplication, and division. The symbols are +-*/, the modulo operator %, and the auto-increment (++) and auto-subtraction (-) operators. The modulo operation applies to integer and character types. Other arithmetic operations apply to all numerical and character types. Others are common sense, but the character types seem odd.

A minus sign (-) is usually used to subtract two numbers, but can also be placed before a number, for example,-a, which indicates that the symbol of a is changed and the original positive number is changed to a negative number, the original negative number will become a positive number, which is also in our common sense.

Modulo (%) is the remainder in mathematics. For example, 5% 3 is, is 0.

Auto-increment (++) and auto-increment (--) are shortcuts. You can add or subtract one for yourself.

In most cases, addition, subtraction, multiplication, division, and intuition are the same and easy to understand. However, there are some things to be aware of, while auto-incrementing and auto-subtraction are slightly more complicated. Let's explain them below.

Considerations for addition, subtraction, multiplication, and Division

During calculation, pay attention to the result range and use the appropriate data type. Both positive numbers can be expressed with int, but the result of multiplication may exceed. The result will be confusing if the result is exceeded. For example:

Int a = 2147483647*2; // 2147483647 is the maximum value that int can represent.

The result of a is-2. Why is-2 not explained for the moment. To avoid this situation, we should use long for the result type, but it is not enough to change it to long, because the operation is still performed according to the int type by default, at least one data is represented as long, that is, L or l is added to the end. The expected result is displayed as follows:

long a = 2147483647*2L;

In addition, it should be noted that the division of integers is not rounding, but directly rounding out the decimal places. For example:

double d = 10/4;

The result is 2 instead of 2.5. If you want to calculate by decimal number, you need to represent at least one number as a decimal number, or use the forced type conversion, that is, add (double) before the number ), it indicates that the number is treated as a double type. Any of the following forms can be used:

double d = 10/4.0;double d = 10/(double)4;

I think there is no special reason for the above considerations. It is probably easier for the language designer to implement the language.

Inaccurate decimal calculation results

Float and double operations may be confusing, for example:

float f = 0.1f*0.1f;System.out.println(f);

This result seems to be self-evident, it should be 0.01, but in fact, the screen output is 0.010000001, followed by 1 more. Use double to see:

double d = 0.1*0.1;System.out.println(d);

The screen output is 0.010000000000000002. After a series of 0 values, the result is not accurate.

What's going on? How can a computer calculate such a simple operation that is not accurate? But this is the case. The reason is that we need to understand the binary representation of float and double, and we will analyze it later.

Auto increment (++)/auto increment (--)

Auto-increment/auto-increment is to add and subtract one for yourself, but each has two forms. One is placed after a variable, such as a ++, --, the other is placed before the variable, for example, ++ a, --.

If you only operate on yourself, there is no difference between the two forms, the difference is that there are other operations. Put it after the variable (a ++), it is to first perform other operations with the original value, and then make modifications to yourself, and put it before the variable (++ ), is to modify yourself first, and then use the modified value for other operations. For example, quick operations and their equivalent operations are:

Quick operations Equivalent operation
B = a ++-1

B = A-1

A = a + 1

A-1 c = ++

A = a + 1

C = A-1

ArrA [I ++] = arrB [++ j]

J = j + 1

ArrA [I] = arrB [j]

I = I + 1

Auto-increment/auto-increment is a "quick" operation that allows programmers to write less code, but unfortunately, due to the strange syntax and strange behavior, it brings some confusion to beginners.

Comparison

The comparison operation calculates the relationship between two values and returns a boolean value. The comparison operation applies to all numeric and character types. The numeric type is easy to understand, but what is the character ratio? Subsequent articles.

Comparison operators include: greater than (>), greater than or equal to (> =), less than (<), less than or equal to (<=), equal to (=), and not equal (! = ).

Most of them are relatively intuitive, and you must note that they are equal.

First, it uses two equal signs = instead of one equal sign (=). Why not use one equal sign? Because an equal sign (=) has been occupied, it indicates a value assignment operation.

In addition, for arrays, = determines whether the two arrays are the same array, rather than whether the elements of the two arrays are the same, even if the content of the two arrays is the same, but if it is two different arrays, = will still return false, as shown below:

Int [] a = new int [] {1, 2, 3}; int [] B = new int [] {1, 2, 3}; // The result of a = B is false.

If you want to compare whether the content of the array is the same, you need to compare each element stored in the array one by one.

Logical operation

Logical operations generate a Boolean value true or false based on the Logical Relationship of data. Logical operations can only be applied to data of the boolean type, but the comparison result is a boolean value. Therefore, the comparison results of other types of data can be used for logical operations.

Logical operators include:

  • And (&): true is true, and false is false if either of them is false.
  • Or (|): if either of them is true, true is true, and false is false.
  • Non (!) : For a variable, true will become false, and false will become true.
  • XOR (^): two identical values are false, and two different values are true.
  • Short circuit and (&): And & similar, the difference is explained immediately
  • Short circuit or (|): similar to |, the difference is explained immediately

Most of the logical operations are intuitive. Note the difference between &, and |. If you only perform logical operations, they are all the same. The difference is that when there are other operations at the same time, for example:

boolean a = true;int b = 0;boolean flag = a | b++>0; 

Because a is true, flag is true, but B's result is 1, because | the formula below is also calculated, even if only a knows the result of flag, still perform subsequent operations. | Is different. If the code of the last sentence is:

boolean flag = a || b++>0; 

Then the value of B is still 0, because | will be "short-circuited", that is, when you see | the previous part can determine the result, ignore | the subsequent operation.

In this example, we can also see that the auto-increment/auto-increment operations are difficult for us. Other operations are dry and brittle. If values are assigned, values are assigned. When values are added, values are compared, it is not mixed together and may write less Code. However, improper use may make it much harder to understand.

Operator priority

A slightly complex operation may involve multiple variables and multiple operations. Which of the following operations is the first and the last one? The Programming Language specifies the priority of different operators, some of which are calculated first, and some are calculated after the meeting. In most cases, this priority is consistent with our common sense understanding.

However, in some complicated cases, we may not understand the operation sequence. However, we don't have to worry about this. We can use parentheses () to express the order we want. The parentheses will be computed first. Simply put, parentheses are used when there is no order.

Summary

This section describes arithmetic operations, comparison operations, and logical operations, but we have left some issues, such:

  • The result of multiplication of positive integers is actually a negative number.
  • The basic decimal operation results are inaccurate.
  • How can I perform arithmetic operations and Comparison on character types?

What's going on?

------------------

Looking at the latest articles, please pay attention to the Public Account "lauma says programming" to explore the essence of Java programming and computer technology. Original article, retain all copyrights.

 

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.