Compare different data types with zero values.

Source: Internet
Author: User

1. bool type: bool type variables cannot be directly compared with TRUE, FALSE, or 1 or 0.

In the bool type, the non-zero value is TRUE, and the zero value is FALSE ). There is no definite value for TRUE. The value of VC ++ is 1, and the value of VB is-1.

The correct comparison method is:

Bool flag = 1;

If flag ){}

If! Flag ){}

2. int type: int type variables are directly compared with 0.

Int a = 1;

If a = 0 ){}

If! = 0 ){}

3. float variable: Do not use "=" or "! = "To compare with any number, we should try to convert it to the form of"> = "and" <=.

Float a = 3/5;

If a >=- value) & (a <= value) {}// value is the allowed precision

4. pointer: Compare pointer variables with NULL.

Because the NULL value of the pointer variable is NULL ):

Int;

Int * p =;

If p = NULL ){}

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.