[Programming specification series] relational expressions/logical expressions in IF Statements

Source: Internet
Author: User

1. Comparison of Boolean variables:

If (FLAG) if (! Flag)

Use non-logical! Operator, because it is suitable for Boolean variables.

 

2. Comparison of integer variables:

If (value = 0) if (value! = 0)

Use normal = and! = Operator.

 

3. Comparison of floating point variables:

If (x = 0.0) // The error may be extremely large. If (x> =-epsinon) & (x <= epsinon ))

Avoid using "=" or "! To compare the value with the number, try to convert it to the "> =" or "<=" format, because the truncation error of the floating point variable is very likely. For example, the result of 1/3*3 is 0. 9999999 ..., if we compare it with 1 again, of course there will be a problem!

I have a deep understanding of this point. I used the following method to solve this problem when I did a computational geometric problem:

If (ABS (x) <EPS) x = 0;

Since X is close to 0, set it to 0 directly.

 

4. Comparison of pointer variables:

If (P = NULL) // Explicit Comparison between P and null emphasizes that p is the pointer variable if (P! = NULL)

Refer to self-developed high quality programming guide-C ++/C language 4.3if statement

 

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.