Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/141.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 琐碎的话题,作业是否返回';假';?_C++ - Fatal编程技术网

C++ 琐碎的话题,作业是否返回';假';?

C++ 琐碎的话题,作业是否返回';假';?,c++,C++,在这个例子中,结果是6。但是i=5不被认为是非零值吗?如果我做I+=5,那么它算作真的。为什么这有什么不同?(也不是,我不是故意把I==5) inti=7; 如果(i=5){ cout赋值返回已赋值的内容。在您的示例中: int i = 7; if (i = 5) { // returns 5, which is non-zero, or "true" cout << ++i; // prints 6, or 5+1 } else { cout << -

在这个例子中,结果是6。但是i=5不被认为是非零值吗?如果我做I+=5,那么它算作真的。为什么这有什么不同?(也不是,我不是故意把I==5)

inti=7;
如果(i=5){

cout赋值返回已赋值的内容。在您的示例中:

int i = 7;
if (i = 5) { // returns 5, which is non-zero, or "true"
    cout << ++i; // prints 6, or 5+1
}  else {
    cout << --i; // would print 4, or 5-1, if it was hit, which it never will
}
inti=7;
if(i=5){//返回5,这是非零的,或“true”

cout您的代码的行为如下:

i = 7;
i = 5;
if ( 5 ) // it's true. Isn't it ?
{
    i = i + 1; // now i is 6
    cout << i;
}
i=7;
i=5;
如果(5)//这是真的,不是吗?
{
i=i+1;//现在我是6

cout赋值操作符,如
=
+=
在对象被赋值后返回对象的值。因此,如果您将某个对象赋值为
false
0
,则可以从赋值操作符获得
false


i=5
的计算结果为
5
,在
if()
的眼中,这是
true
。但是
i=0
的计算结果为
0
,而
if()

6不是预期的结果吗?表达式
i=5
非零,
的结果在stackoverflow上以前没有回答过相同的问题。这是。只是一个提示:如果它很琐碎,可能以前问过。搜索可能会为您节省一些否决票。
int i = 7;
if (i = 5) { // returns 5, which is non-zero, or "true"
    cout << i++; // prints 5, i is 6 after this line
}  else {
    cout << i--; // would print 5, but i is 4 after this line
}
i = 7;
i = 5;
if ( 5 ) // it's true. Isn't it ?
{
    i = i + 1; // now i is 6
    cout << i;
}