guhet5ty3984wefjn
guhet5ty3984wefjn

Reputation: 53

Using ++ operator leads to unexpected results

I do not understand why the two codes below lead to the same result. Shouldn't the latter print from 6 to 10 since n is incremented first?

Edit: I used both Visual Studio 2019 and repl.it.

#include <iostream>
using namespace std;

int main() {
    int n=5;
    for (n; n<10; n++)
        cout << n << endl;
    return 0;
}

>>> 5
6
7
8
9

#include <iostream>
using namespace std;

int main() {
    int n=5;
    for (n; n<10; ++n)
        cout << n << endl;
    return 0;
}

>>> 5
6
7
8
9

Upvotes: 0

Views: 64

Answers (3)

Keith Thompson
Keith Thompson

Reputation: 263237

The only difference is this:

    for (n; n<10; n++)

vs. this:

    for (n; n<10; ++n)

The first and third expressions in a for statement are evaluated only for their side effects, with any result discarded. (The second is evaluated and its result used to determine whether to continue the loop.) The only difference between n++ and ++n is their results; their side effects are identical. So in this context n++ and ++n are effectively identical.

I'll also mention that the first expression n has no side effects, so it might well be omitted:

    for (; n<10; n++)

Or, better yet, you could incorporate the declaration and initialization into the loop, replacing the existing declaration int n=5;:

    for (int n = 5; n < 10; n++)

Upvotes: 0

Harshit Gakhar
Harshit Gakhar

Reputation: 36

There's Nothing wrong here, its just how its supposed to work

Firstly, you should know a for loop works for loop, without loss of generality can be written as:

for(exp1; exp2; exp3)
{
    //body
} 

here exp1 will be executed only when entering the loop first time, then exp2 will be evaluated, if true loop will continue, otherwise not. exp3 is evaluated only after each iteration is complete.

also the only difference between i++ and ++i, is when you assign it to something. say i is 5, then after statement

i++;

i will be 6 which is also true for this statement:

++i;

but say i=5, but you try something like this:

a=i++;

after this statement, a will be 5, and i will be 6.

but after this statement, with same initial condition(i = 5):

a=++i;

both a and i will be 6.

Upvotes: 0

JohnFilleau
JohnFilleau

Reputation: 4288

From https://en.cppreference.com/w/cpp/language/for

attr(optional) for ( init-statement condition(optional) ; iteration_expression(optional) ) statement

The above syntax produces code equivalent to:

{
    init_statement
    while ( condition ) {
        statement
        iteration_expression ;
    } 
}

Your two for loops are therefore equivalent to

{
    n;
    while (n < 10) {
        cout << n << endl;
        n++;
    }
}

and

{
    n;
    while (n < 10) {
        cout << n << endl;
        ++n;
    }
}

In a for loop:

  1. The first statement is executed exactly once
  2. The conditional is checked, and if true
  3. The body is executed
  4. Then the iteration expression is executed
  5. Goto (2.)

If you want your loop to start at 6, you need to explicitly tell it to start at 6.

Upvotes: 4

Related Questions