0"?-6ren"> 0"?-最近继承了一个老项目,做一些优化和添加新功能。在这个项目中,我在代码中看到了这种类型的情况: if (int_variable) 代替 if (int_variable > 0) 我只对 boolea-6ren">
gpt4 book ai didi

c++ - 在条件中省略 "> 0"?

转载 作者:塔克拉玛干 更新时间:2023-11-03 08:06:30 25 4
gpt4 key购买 nike

最近继承了一个老项目,做一些优化和添加新功能。在这个项目中,我在代码中看到了这种类型的情况:

if (int_variable)

代替

if (int_variable > 0)

我只对 boolean 类型的变量使用了第一个选项。

您认为第一个选项是检查数字是否为正数的“正确”方法吗?

最佳答案

负数的计算结果也为 true,因此您应该使用 if (int_variable > 0) 来检查数字是否为正数。

关于c++ - 在条件中省略 "> 0"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33543581/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com