Hi all,

Can anyone explain to me in details preferable with an example the different between i++ and i++ please.

Here my example with what I understood:

for (int (0); i<10; i++) {
cout << i++;
cout << ++i;
}

What is the output?

Is the working steps like this:

When i =0;
for (i = 0; 0 < 10; 0) //True 0 <10 do the following
output: 0 //0 + 1
output: 2 //1 + 1


When i =2;
for (i = 2; 2 < 10; 2) //True 2 <10 do the following
output: 2 //2 + 1
output: 4 //3 + 1


When i =4;
for (i = 4; 4 < 10; 4) //True 4 <10 do the following
output: 4 //4 + 1
output: 6 //5 + 1


When i =6;
for (i = 6; 6 < 10; 6) //True 6 <10 do the following
output: 6 //6 + 1
output: 8 //7 + 1


When i =8;
for (i = 8; 8 < 10; 8) //True 8 <10 do the following
output: 8 //9 + 1
output: 11 //10 + 1

So the output would be: 022446811

Can anyone please help me if this is a correct output.
Thanks

Recommended Answers

All 4 Replies

Can anyone explain to me in details preferable with an example the different between i++ and i++ please.

The standalone difference is as functionally as follows:

// A function that performs ++i
int prefix_increment(int& i)
{
    i = i + 1;

    return i;
}

// A function that performs i++
int postfix_increment(int& i)
{
    int temp = i;

    i = i + 1;

    return temp;
}

So the output would be: 022446811

You are forgetting the i++ in the for() .

Why don't you just 'ask' your compiler?

You are forgetting the i++ in the for() .

Why don't you just 'ask' your compiler?

Thanks well I rather not using the compiler otherwise i won't understand the concept if I rely on compiler.

But thanks..just that I seem mixed up between these two operators.

Thanks guys I think I managed to work it out..! Many thanks for the help!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.