Classic good standard style if conditional statement written -C / C ++ program basis (four)

table of Contents

 

Boolean variable

Integer variable

Floating-point type variable

Pointer variable type


Boolean variable

Assume Boolean variable name for the flag, which is compared with the standard value of zero if the statement is:

#include<iostream>
#include <stdio.h>

int main()
{
	bool flag = false;
	if (flag) { printf("good\n"); };  //非标准风格:if (flag == true) { printf("good\n"); };
	if (!flag) { printf("well\n"); }; //非标准风格:if (flag == false) { printf("good\n"); };
	system("pause");
	return 0;
}

This style, according to the Boolean type semantics zero value "false" (to false), any non-zero value is "true" (true). Why would you write this style as standard, because on different platforms, the definition of True is not uniform . For example, the Visual studio there really is defined as 1, but in which Visual basic, really is defined as -1.

Integer variable

Assuming that integer variable name to value, which is compared with the standard value of zero if the statement is:

#include<iostream>
#include <stdio.h>

int main()
{
	int value = 1;
	if (value == 0) { printf("good\n"); }; //非标准风格:if(value);
	if (value != 1) { printf("well\n"); }; //非标准风格:if(!value);
	system("pause");
	return 0;
}

The above comment section is defined integer type of the if statement when the usual style, so defined, it is easy for the reader to confuse data definitions and data type bool defined. 

Floating-point type variable

Assume the name of floating-point type variable is x, compared with a value of zero (0.0) standard if statement is:

#include<iostream>
#include <stdio.h>

int main()
{
	float x = 1.0;
	float EPSINON = 0.001;
	if ((x >= -EPSINON)&&(x <= EPSINON)) { printf("good\n"); }; // 非标准风格:if(x == 0.0);
	if ((x < -EPSINON)||(x > EPSINON)) { printf("well\n"); }; //非标准风格:if(x != 0.0);
	system("pause");
	return 0;
}

It should make clear that, in the common data types inside, float and double two data types, are all precision restrictions , so, in the face with a comparison of floating-point type or time of judgment, must be avoided with the form: "! =" "==", this type of comparison because, with too many digits behind the decimal floating-point data, it is difficult to find two exactly the same, of course, if you are required to complete each the same number of bits that need to provide it too well!

Pointer variable type

Pointer variable p and the zero value comparison:

#include<iostream>
#include <stdio.h>

int main()
{
	int* p = NULL;
	if (p == NULL) { printf("good\n"); }; //非标准风格:if(p == 0);
	if (p != NULL) { printf("well\n"); }; //非标准风格:if(p != 0);
	system("pause");
	return 0;
}

For a pointer variable, the zero value is "NULL", meaning maybe represented is completely different, if p and compare NULL, emphasized p is a pointer variable; if p 0 and compares it is easy to think that p an integer variable.

 

Published 271 original articles · won praise 8 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_17846375/article/details/104879797