the program behaviour is different with code that have similar function

this

#include <iostream>
#include <string>

int main()
{
    int a = 1;
    while (a < 11)
    {
        int b = a++;
        int c = a * b;
        std::cout << c << std::endl;
        ++a;

    }
    return 0;
}

/* result:
 * 2
 * 12
 * 30
 * 56
 * 90
 */

and this one

#include <iostream>
#include <string>

int main()
{
    int a = 1;
    while (a < 11)
    {
        int b = a + 1;
        int c = a * b;
        std::cout << c << std::endl;
        ++a;

    }
    return 0;
}

/* result:
 * 2
 * 6
 * 12
 * 20
 * 30
 * 42
 * 56
 * 72
 * 90
 * 110
 */

I thought that only ++a would not work because it's equivalent as "a = a + 1"
while a++ would equivalent "a + 1", or what I miss?

-vastor

You should try something like this.

int x = 0;

std::cout << x++ << std::endl;

int y = 0;

std::cout << ++y << std::endl;

What's the result?

I thought that only ++a would not work because it's equivalent as "a = a + 1"
while a++ would equivalent "a + 1", or what I miss?

You missed the fact that
1) a+1 simply means add 1 to a and use the result
2) a++ means a=a+1 and for subsequent uses of a use the new value.

IOW, a+1 doesn't change a whereas a++ does.

You missed the fact that
1) a+1 simply means add 1 to a and use the result
2) a++ means a=a+1 and for subsequent uses of a use the new value.

IOW, a+1 doesn't change a whereas a++ does.

wait a minute!!

1) got there, done that!
2) ermmm, really? I thought the "a = a + 1" equivalent for "++a" NOT "a++", or maybe there hidden meaning in given description from accelerated c++...
3) if what u say is true about "a++" equal "a = a +1", then what is ++a equivalent for?

x++ Increments x, returning the original value of x
x-- Decrements x, returning the original value of x

"there anything I miss here?" -vastor

++x Increments x, returning the incremented value
--x Decrements x, returning the decremented value

btw, for gerard, the result is:-

0
1

where 0 equals x++; where why I thought this equal "b = c + 1"
and 1 equals ++y; where why I thought this equal "c = c + 1"

-vastor

a++ and ++a alone mean the same thing. When used in an equation, they are slightly different.

Run this and figure it out:

int a,b,c;

a = 5;
b = 5;

c = a++;
cout << "c=" << c << "  a=" >> a << endl;

c = ++b;
cout << "c=" << c << "  b=" >> b << endl;

It's quite easy to design a test like this to answer questions yourself...

get it now, thnx all

for WaltP,

all this while, I thought that a++ is the "short form" of expression of "a + 1", read on some tutorial website, I think I should not eat all what random article said.. x(

about design to figure out, sometime my brain malfunction and end up lazy to figure out myself, sorry for any problem here... I will try thinking harder next time before come to this forum for help.

ty again for all answer... ^_^"

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.