PDA

View Full Version : C++ Programming



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++.

tomwakefield
7th January 2005, 05:34 AM
n++ and ++n are shorthand ways of saying n = n + 1 i.e. take the variable 'n', add 1 to it, and give 'n' that new value.

So n2 = ++n1 is saying take n1, add one to it, give n1 that new value, and then give n2 the current value of n1

and n3 = n1++ is saying take n1, give n3 the current value of n1 and then add 1 to n1 and give n1 that new value

With the first
n2 = ++n1
The process is basically as follows.
n1 = 5
n1 = n1 + 1
.: n1 = 6
n2 = n1
.: n2 = 6

With the second
n1 = 5
n3 = n1
.: n3 = 5
n1 = n1 + 1
.: n1 = 6

Hopefully that will help. I'm not entirely sure I understand what you need help with though.

Amended - even I make mistakes sometimes :whistilin

Another note: I never use ++n.

Intel_Hydralisk
7th January 2005, 06:47 AM
It has to do with how the variables are fetched from RAM. When declaring the variables n1, n2, and n3, there is a spot in RAM created for each one which stores the value of the variable. In the first case, the value of n1 in memory is changed before the equal sign operator is applied. In the second case, the equal sign operator is applied before the value of n1 in RAM is changed. That's just how it works... but I personally think it's confusing at first and may mess you up. To make sure that your programs work right at first, don't use shorthand notation like that.

Xv_raven_vX
7th January 2005, 10:46 AM
So n2 = n1++ is saying take n1, add one to it, give n1 that new value, and then give n2 the current value of n1



// the value of n1 is 6 but the value of n3 is 5
n1 = 5;
n3 = n1++;


I don't understand how n3 = 5.

n3 = n1++;

You just got through explaining to me how n1++ means take n1 and add one to it. So, if that's the case shouldn't n3 equal 6?

tomwakefield
7th January 2005, 12:27 PM
Sorry, I got them the wrong way round. I'll amend my original post.

Flocito
7th January 2005, 12:28 PM
edit: Nevermind....tom cleared it up.

Xv_raven_vX
7th January 2005, 12:48 PM
Sorry, I got them the wrong way round. I'll amend my original post.

What do you mean the wrong way around? You got them backwards? I still don't understand the concept of n1++. What does n1++ do? ++n1 adds one to the value of n1. So n1++ can't do the same thing. The book doesn't clear this up that well so I'm relying on an answer here.

Flocito
7th January 2005, 01:40 PM
What do you mean the wrong way around? You got them backwards? I still don't understand the concept of n1++. What does n1++ do? ++n1 adds one to the value of n1. So n1++ can't do the same thing. The book doesn't clear this up that well so I'm relying on an answer here.
n1++ does do the same thing and ++n1, with the only difference being when the incrementing takes place.

++n1 increments the variable n1 and then assigns it.
n1++ assigns the variable and then increments it.

n2=++n1 is the same as writing:
n1=n1+1;
n2=n1;

n2=n1++ is the same as writing:
n2=n1;
n1=n1+1;

Xv_raven_vX
7th January 2005, 03:20 PM
OOoo

Alright well I feel like a dumbass in a way, but the book didn't really explain those two concepts that well so it the book's fault. Thanks for the help. I'll post some more stuff if I don't understand it.

White_Hindu
7th January 2005, 06:26 PM
I believe you can use the 4 following statements have identical ending values:


//n=5, end result is always 6

n++;

++n;

n=n+1;

n+=1;

"+=t" always adds the value of t to the original value.


"n=n+t" is pretty much self explanitory. This always means the value of n becomes equal to the current value of n plus the value of t.

Just so you know, when you see "=", it is always used as an arethmetic operator, and it means "Becomes equal to". If you are checking to see if 2 variables are the same value, you have to use the boolean operator "==". But that's beside the point. You may just have to remember that, it gave me a bit of trouble in computer science.

**Edited for accuracy

Flocito
7th January 2005, 08:02 PM
I believe you can use the 4 following statements have identical ending values:


//n=5, end result is always 6

n++;

++n;

n=n+1;

n+=1;

"+=t" always adds the value of t to the original value.


True, White_Hindu, in the case you presented all the values for n will have the same value, but the important thing to note from the examples that Xv_raven_vX gave is that even though all those operators increment the value of n, they do not all do it the same way. This is an important concept to understand otherwise you might code something up like:

n1 = n++;
or
n1 = ++n;

and not know why you aren't getting the value you are expecting in your variable.



++ always adds 1, but you can also use "++t" or "t++" to add the value of t.


I'm sorry but this is not a true statement. If you want to add the value of t you can use n+=t; but ++t and t++ will not add the value t to your variable n, as in you wrote the code n++t;

I'm also pretty sure that if you assigned t a number like 5, if you wrote 5++; that would return a syntax error. And you couldn't write something like n++5; Now you can overload the ++ operator to do whatever wanted, ie increment by 5, but it is a uniary operator so it would only be able to operate on one input.

Don't worry if this is confusing Xv_raven_vX, it will make more sense later when you learn more about C++.

Edit: apparently you figured it out, but I'm going to leave this anyway just in case anyone else got confused.


Yeah, scratch what I said about that, I'm remembering my Java days, which is similar, but not identical to C++.

And my deely about t++ was supposed to be in the context of looping:

for(int t=0;t<10;t++2)
System.out.print("*");

Different programming language, but t++2 is supposed to increment the counter by 2 rather than 1 each iteration. You can use any number really, but in this example, it would only print 5 asterisks rather than 10 asterisks. Sorry for going off on the tangent, I thought it was somewhat relavent to the conversations as looping in Java and C++ is almost identical.
Have you tried coding that in C++, because it's not going to work for reasons I mentioned above. ++ is a uniary operator, so it can't operate on t and 2 in your for loop. You will get a syntax error. If you wanted to increment that loop by 2 you would have to write:

for(int t=0;t<10;t+=2)

Xv_raven_vX
7th January 2005, 09:41 PM
True, White_Hindu, in the case you presented all the values for n will have the same value, but the important thing to note from the examples that Xv_raven_vX gave is that even though all those operators increment the value of n, they do not all do it the same way. This is an important concept to understand otherwise you might code something up like:

n1 = n++;
or
n1 = ++n;

and not know why you aren't getting the value you are expecting in your variable.


I'm sorry but this is not a true statement. If you want to add the value of t you can use n+=t; but ++t and t++ will not add the value t to your variable n, as in you wrote the code n++t;

I'm also pretty sure that if you assigned t a number like 5, if you wrote 5++; that would return a syntax error. And you couldn't write something like n++5; Now you can overload the ++ operator to do whatever wanted, ie increment by 5, but it is a uniary operator so it would only be able to operate on one input.

Don't worry if this is confusing Xv_raven_vX, it will make more sense later when you learn more about C++.

Edit: apparently you figured it out, but I'm going to leave this anyway just in case anyone else got confused.


Have you tried coding that in C++, because it's not going to work for reasons I mentioned above. ++ is a uniary operator, so it can't operate on t and 2 in your for loop. You will get a syntax error. If you wanted to increment that loop by 2 you would have to write:

for(int t=0;t<10;t+=2)

To my surprise, I understood you quite clearly. I have already learned about the difference between the 2 equal signs. '=' Is called an assignment operator. ' == ' is the equality operator. The greater than, less than signs are pretty much self explanitory. In fact, as of right now I am reading about the "Simple Logical Operators" and "Storing logical values."

You cleared up my confusion so I am now able to understand then a lot better. So thanks for your help. If I get lost again, I'll get back on.