简体   繁体   English

(关于i++的问题)这段代码的原理是什么?

[英](Question about i++) What is the principle of this code?

code代码

#include <iostream>
using namespace std;

int main()
{
    int i = 1;
    while (i < 10)
        if (i++ % 2 == 0)
            cout << i << endl;

    return 0;
}

The output is输出是

3
5
7
9

Since i is 1, I thought that the if statement satisfies 2% 2 == 0 and 2 should be output, but I don't know why 3.由于i是1,我以为if语句满足2% 2 == 0,应该输出2,但是不知道为什么是3。

if (i++ % 2 == 0) means evaluate i%2 and compare to 0 , and increment i at some point after evaluating it but before cout << i if (i++ % 2 == 0)的装置评价i%2 ,并比较0 ,并递增i评估它之后在某些点cout << i

That's why you're seeing odd numbers printed.这就是为什么您会看到打印出奇数的原因。 In every case, an even value of i caused the statement controlled by the if to be executed, and the increment changed the even value to an odd one.在任何情况下, i的偶数值都会导致if控制的语句被执行,并且增量将偶数值更改为奇数。

You are using post-increment , which increments the variable and then returns the old value before the increment.您正在使用post-increment ,它增加变量,然后在增加之前返回值。 You are then printing the incremented value.然后您正在打印增量值。

So, lets look at your loop step-by-step:所以,让我们一步一步地看看你的循环:

  • On the 1st iteration, i is 1. i++ sets i to 2 , but returns 1 .在第一次迭代中, i为 1。 i++i设置为2 ,但返回1 So the if compares 1 % 2 == 0 , which is false.所以if比较1 % 2 == 0 ,这是错误的。

  • On the 2nd iteration, i is 2. i++ sets i to 3 , but returns 2 .在第二次迭代中, i为 2。 i++i设置为3 ,但返回2 So the if compares 2 % 2 == 0 , which is true, so cout << i prints 3 .所以if比较2 % 2 == 0 ,这是真的,所以cout << i打印3

  • On the 3rd iteration, i is 3. i++ sets i to 4 , but returns 3 .在第三次迭代中, i是 3。 i++i设置为4 ,但返回3 So the if compares 3 % 2 == 0 , which is false.所以if比较3 % 2 == 0 ,这是错误的。

  • On the 4th iteration, i is 4. i++ sets i to 5 , but returns 4 .在第 4 次迭代中, i为 4。 i++i设置为5 ,但返回4 So the if compares 4 % 2 == 0 , which is true, so cout << i prints 5 .所以if比较4 % 2 == 0 ,这是真的,所以cout << i打印5

  • And so on...等等...

It sounds like perhaps you were expecting i++ to pre-increment instead, returning the new value rather than the old value.听起来您可能希望i++进行预递增,而是返回值而不是值。 In which case, there is a slightly different syntax for that purpose:在这种情况下,为此目的的语法略有不同:

#include <iostream>
using namespace std;

int main()
{
    int i = 1;
    while (i < 10)
        if (++i % 2 == 0) // <-- ++i vs i++
            cout << i << endl;

    return 0;
}

Note the position of ++ in relation to i .注意++相对于i的位置。 Both syntaxes increment i , but positioning ++ to the left of i returns the new value after i is incremented, while positioning ++ to the right of i returns the old value before i was incremented.两种语法增加i ,但定位++到左边的i返回后的i递增,虽然定位++到右边的i回到以前的i被递增。

Same with the decrement operator -- , too.与递减运算符--相同。 It has pre-decrement and post-decrement versions.它有减量前和减量版本。

According to the C++ 14 Standard (5.2.6 Increment and decrement)根据 C++ 14 标准(5.2.6 递增和递减)

1 The value of a postfix ++ expression is the value of its operand. 1后缀++表达式的值是其操作数的值。 [ Note: the value obtained is a copy of the original value — end note ] The operand shall be a modifiable lvalue. [注意:获得的值是原始值的副本——尾注] 操作数应为可修改的左值。 The type of the operand shall be an arithmetic type or a pointer to a complete object type.操作数的类型应为算术类型或指向完整对象类型的指针。 The value of the operand object is modified by adding 1 to it , unless the object is of type bool, in which case it is set to true.操作数对象的值通过向它加 1 来修改,除非对象是 bool 类型,在这种情况下它被设置为 true。

So for example in the second iteration of the while loop the variable i is indeed equal to 2 after incrementing it in the first iteration of the loop因此,例如在 while 循环的第二次迭代中,变量i在循环的第一次迭代中递增后确实等于2

while (i < 10)
    if (i++ % 2 == 0)
        cout << i << endl;

Thus the condition of the if statement因此 if 语句的条件

    if (i++ % 2 == 0)

evaluates to true.评估为真。 But the value of the variable i after the evaluation of the condition was incremented.但是在评估条件后变量i的值增加了。 So this statement所以这个说法

        cout << i << endl;

outputs the new value 3.输出新值 3。

You can equivalently rewrite the while loop the following way您可以通过以下方式等效地重写 while 循环

while (i < 10)
    if (i % 2 == 0)
        cout << ++i << endl;
    else
        ++i;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM