gpt4 book ai didi

c++ - 如果没有条件?

转载 作者:可可西里 更新时间:2023-11-01 15:35:55 25 4
gpt4 key购买 nike

我今天才发现这个“C++”,但我无法理解它:

if(array[i][j]) {--i;--j;}

谁能给我解释一下这是怎么回事?我只是不明白。这里的条件是什么?似乎每次都是如此,但是当我摆脱 IF 时(所以只剩下这个 {--i;--j;}。)它的工作原理就不一样了。

我是 C++ 的新手,所以请轻松阅读解释! :)

谢谢!

最佳答案

在 C++ 中,非零值可用于在条件语句中指示“成功”。

这是来自 C99,第 6.8.4.1 节:

The if Statement

2 In both forms, the first substatement is executed if the expression compares unequal to 0.

来自 C++03,第 6.4 节

4 The value of a condition that is an initialized declaration in a statement other than a switch statement is the value of the declared variable implicitly converted to type bool. If that conversion is ill-formed, the program is ill-formed. The value of a condition that is an initialized declaration in a switch statement is the value of the declared variable if it has integral or enumeration type, or of that variable implicitly con- verted to integral or enumeration type otherwise. The value of a condition that is an expression is the value of the expression, implicitly converted to bool for statements other than switch; if that conversion is ill-formed, the program is ill-formed. The value of the condition will be referred to as simply “the condi- tion” where the usage is unambiguous.

bool 转换定义在6.3.1.2

6.3.1.2 Boolean type

1 When any scalar value is converted to _Bool, the result is 0 if the value compares equal to 0; otherwise, the result is 1.

int是一个标量类型。我假设这就是你的数组填充的内容,因为你可以在它们上使用一元 ++--

关于c++ - 如果没有条件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4286535/

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