When determining conditions, we often encounter comparisons with 0 values. However, many people usually use improper conditions to make judgments. Next we will discuss in detail the various situations:
1. Comparison between Boolean variables and zero values:
If the variable name of a Boolean variable is flag, the condition judgment statement for comparing it with 0 is as follows:
If (flag) // indicates that the flag is true.
If (! Flag) // indicates that the flag is false.
Cause: According to the Boolean semantics, 0 is false, and any non-0 is true. Some people may think that the following method can be used directly,
If (flag! = 0) // indicates that the flag is true.
If (flag = 0) // indicates that the flag is false.
According to the definition, it seems to be correct. Of course, it will not be wrong in the program, but we will think this is a comparison between the boolean type and the integer type.
Not matched! Of course, there is also the following method.
If (flag = TRUE) // indicates that the flag is TRUE.
If (flag = FALSE) // indicates that the flag is FALSE.
Yes, for many languages, these two keywords are available, and FALSE is defined as 0, but as long as TRUE, most languages define it
Is 1, but this does not follow the definition of Boolean Type (non-zero represents true), such a comparison will have a bug, such as when flag = 2, if the flag is true, the statement will not be executed.
To sum up, the comparison between Boolean variables and 0 values should be written as follows:
If (flag) // indicates that the flag is true.
If (! Flag) // indicates that the flag is false.
2. Comparison between Integer Variables and zero values:
In this case, it is easy to write the following code:
If (flag! = 0) // indicates that the flag is true.
If (flag = 0) // indicates that the flag is false.
3. Comparison between floating point and zero value:
Comparing floating-point numbers with zero values is more complex. Because the computer indicates that floating point numbers have a precision limit. For floating-point numbers that exceed the Precision Limit
It truncates decimal parts other than their precision. Therefore, the two floating point numbers that are originally not equal may become equal in the computer.
Float a = 2.111111112;
Float a = 2.111111118;
Theoretically, the two numbers are not equal, but 32 is equal for the machine (reason: On a 32-bit machine, float retains 6 as decimal places ).
Therefore, for floating-point comparison, it is stipulated that if the absolute values of the two floating-point numbers with the same symbol are destroyed or an acceptable error (that is, the accuracy) is equal
They are equal, otherwise they are not equal. The precision should be determined according to specific requirements. Instead of using "=" or "! = "Compares two floating point numbers.
Float sub = 0.0000001f; // custom precision
If (abs (a-B) <= sub); // indicates a = B;
If (abs (a-B)> sub); // indicates! = B;
The comparison with zero value is of course the following statement:
If (abs (a) <= sub); // indicates a = 0;
If (abs (a)> sub); // indicates! = 0;
Note: In the actual programming environment, if you directly compare a floating point with another number (integer or floating point number) is equal (=) or not (! =). Errors may occur.
The results may depend on the specific compilation environment and platform, because each compilation platform has its own default precision, which is directly equal to = and! = Comparison
This default precision is used, rather than the difference between two bit values in the memory.
4. Comparison between pointer variables and zero values:
The zero value of the pointer variable is "NULL" (that is, NULL), that is, it does not point to any object.
Therefore, the standard if statement for comparing pointer variables with zero values is as follows:
If (p = NULL) // p is NULL
If (p! = NULL) // p is not empty
Although the value of NULL is the same as that of 0, they have different meanings. Enable the NULL definition in the VC environment:
[Cpp]
/* Define NULL pointer value */
# Ifndef NULL
# Ifdef _ cplusplus
# Define NULL 0
# Else
# Define NULL (void *) 0)
# Endif
# Endif
/* Define NULL pointer value */
# Ifndef NULL
# Ifdef _ cplusplus
# Define NULL 0
# Else
# Define NULL (void *) 0)
# Endif
# Endif
That is to say, NULL is a defined macro, so that the comparison between integer and 0 can be distinguished;
If you use the following statement:
If (p! = 0) // indicates that the flag is NULL.
If (p = 0) // indicates that the flag is NULL.
Of course, this will not be wrong, but we will mistakenly think that p is an integer variable.
Although this is a few small aspects, the bugs that are difficult to find in the program often appear in these details!