c++中if语句的模行为
Modulo behaviour with if statement in C++
为什么这段代码返回5。首先,if返回5%2 = 1,if(1)返回false。为什么呢?
#include <iostream>
using namespace std;
int fn_test(int i=5)
{
if(i%2) return i++;
else
return fn_test(i-1);
}
int main()
{ int test=fn_test(5);
cout<< " this is out put " << test;
return 0;
}
您注意到5%2
的结果是1
,它是非零的,因此是"true"。只有零是"假",其他都是"真"。
这意味着你做return i++
,它返回i
(即5
)的旧值,然后再增加i
。
相关文章:
- 我的简单if-else语句是如何无法访问的代码
- Insert函数不适用于2 if语句C++
- If语句未被求值C++
- C++嵌套if语句,基本货币交换
- 多个If语句与使用逻辑运算符计算条件的单个语句的比较
- 是否可以使用if constexpr删除控制流语句
- 我似乎对if/else的基本语句有问题:/
- If语句在c++中被忽略
- 比较if语句中的数组值和int值
- 以在Qt中的IF语句中设置时间延迟
- Craps游戏问题,忽略if语句
- c++:定义if语句中的模板
- C++嵌套的 If 语句中,它无法按预期工作
- 如果中的多个语句是否与多个 if 相同?
- C++ If/Else 语句被跳过
- 使用语句if时逻辑的问题
- 如何添加语句if-else
- 如何将语句if(isalpha(c))从c++转换为MIPS ?
- 在if语句if C++中创建对象时出现作用域问题
- if语句-if/else和参数放置C++