Xv_raven_vX

7th January 2005, 01:36 AM

When I am older I plan to become some type of computer programmer, I haven't decided which type yet. A few days ago my dad got me a C++ programming book which teaches me the basics of the C++ language to the hardest stuff. 2 days ago, I just made my first executable which converts any Celsius value to a Fahrenheit value. As of right now I am learning about variables and what their purposes are and I was wondering if I could get some help because I don't understand something that's written in the book:

The increment and decrement operators are peculiar in that both come in two flavors: a prefix version and a postfix version (known as pre-increment and post-increment, respectively). Consider, for example, the increment operator (the decrement works in exactly the same way).

Suppose that the variable 'n' has the value 5. Both ++n and n++ increment 'n' to the value of 6. The difference between the two is that the value of ++n is an expression is 6 while the value of n++ is 5. The following example illustrates this difference:

// declare three integer variables

int n1, n2, n3;

// the value of both n1 and n2 is 6

n1 = 5;

n2 = ++n1;

// the value of n1 is 6 but the value of n3 is 5

n1 = 5;

n3 = n1++;

Thus is given the value of n1 after n1 has been incremented (using the pre-increment operator), whereas n3 gets the value of n1 before it is incremented using the post-increment operator.

Now then, I understand what each symbol and abreviation means. However, I need a better explination of ++n and n++. It says in the second part of that code that n1 = 5 and n2 is adding 1 to n1. But in the last part I can't understand it. How does n++ contribute to 5? How is the value of n3 5?

Before I can go further into the book I will need to understand this first. No one in my family understand it and the book isn't giving me much of an explination on them. So I'm hoping someone here knows a bit of C++.

The increment and decrement operators are peculiar in that both come in two flavors: a prefix version and a postfix version (known as pre-increment and post-increment, respectively). Consider, for example, the increment operator (the decrement works in exactly the same way).

Suppose that the variable 'n' has the value 5. Both ++n and n++ increment 'n' to the value of 6. The difference between the two is that the value of ++n is an expression is 6 while the value of n++ is 5. The following example illustrates this difference:

// declare three integer variables

int n1, n2, n3;

// the value of both n1 and n2 is 6

n1 = 5;

n2 = ++n1;

// the value of n1 is 6 but the value of n3 is 5

n1 = 5;

n3 = n1++;

Thus is given the value of n1 after n1 has been incremented (using the pre-increment operator), whereas n3 gets the value of n1 before it is incremented using the post-increment operator.

Now then, I understand what each symbol and abreviation means. However, I need a better explination of ++n and n++. It says in the second part of that code that n1 = 5 and n2 is adding 1 to n1. But in the last part I can't understand it. How does n++ contribute to 5? How is the value of n3 5?

Before I can go further into the book I will need to understand this first. No one in my family understand it and the book isn't giving me much of an explination on them. So I'm hoping someone here knows a bit of C++.