Comparison of conditional judgment statements

Source: Internet
Author: User

Comparison of conditional judgment statements

Process Control to make conditional statement judgment, often use a variety of data types of variables and 0 value comparison problem, here is a summary to deepen the understanding of the data type, non-canonical and 0 comparison statements are easy to participate in the comparison of the data types are misunderstood.

§ 1. boolean variable vs. 0 value comparison

bool c99 standard has Boolean type _bool 0 true, 0 for false, if your compiler does not support Boolean types, you can customize the type

Boolean variables cannot be directly compared to TRUE,FALSE , or 1,0 , assuming that the Boolean type is named flag,

The standard if statement that it compares to the 0 value is as follows:

IF (flag)// indicates that flag is true

If (!flag)// indicates flag is False

§ 2. Comparison of integer variables with 0 values

Integer variables should be applied "= =" or "! ="compare directly with 0. Assuming that the integer variable is named value,

It compares the standard if statement with the 0 value as follows:

If (value = = 0)

If (Value! = 0)

§ 3. floating-point variable versus 0-value comparison

There is a precision limit for variables of type float or double , so be sure to avoid using floating-point variables

"= =" or "! ="In comparison with numbers, you should try to convert them into">="or"<="forms. Because float will have an error.

For example, if you assign a float a=0, a may be 0.000000001, there is an error, so use if( x = = 0) to judge, often will not set up. the float_accuracy is the allowable error, the precision, and the standard statement that the floating-point variable a compares to the 0 value is:

Const float float_accuracy = 0.00001;

If ((a >=-float_accuracy) && (a <= float_accuracy))

§ 4. pointer variable versus 0 value comparison

The 0 value of the pointer variable is null (that is, null), although The value of NULL is the same as 0 , but the two have different meanings. Assuming that the pointer variable is named p, the standard statement that compares it to the 0 value is as follows:

If (P = = null)//p is explicitly compared with NULL , emphasizing that p is a pointer variable

If (P! = NULL)

if (null = = P) This is written by the programmer in order to prevent the if (p = = null ) mistakenly writing if (p = null) and intentionally reversed, So this kind of writing is a certain advantage.


Comparison of conditional judgment statements

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.