它的价值是更好地使用?布尔真或整数1? [英] Which value is better to use? Boolean true or Integer 1?
问题描述
这有什么意义或没有?
推荐答案
一个布尔值真正
,没错,是一个布尔值。使用它的只要你想离preSS某一个二进制条件满足的
A boolean true
is, well, a boolean value. Use it whenever you want to express that a certain binary condition is met.
文字整数 1
是一个数字。使用它,只要你指望什么。
The integer literal 1
is a number. Use it, whenever you are counting something.
不要使用整数的布尔值,反之亦然。它们是不同的。
Don't use integers for booleans and vice versa. They are different.
考虑一个变量 INT isEnabled
。当然,我可以猜测, 0
和 1
可能是这个变量的唯一预期值。但是,语言的角度来看,没有什么能够阻止我从分配 4247891
。使用布尔,但是,制约有效的值真正
和假
。这没有留下炒作空间。
Consider a variable int isEnabled
. Of course, I can guess that 0
and 1
may be the only intended values for this variable. But language-wise, nothing keeps me from assigning 4247891
. Using a boolean, however, restricts the valid values to true
and false
. This leaves no room for speculation.
(C ++廉政局和布尔变量有些敞篷车,但它通常皱眉)
(C++ int's and bools are somewhat convertible, but it's generally frowned upon)
这篇关于它的价值是更好地使用?布尔真或整数1?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!