Win a copy of Functional Reactive Programming this week in the Other Languages forum!

# Beginner here needing some clarification on assignment =

Daniel Rich
Greenhorn
Posts: 16
Hi, first let me say I'm completely new to Java and it has been at least 10 years since I tinkered with a computer programming (I used to code quiet a bit in VB). A lot of the books I read said it would be a great idea to sign up to a community and it would help, so here I am. Nice to meet you all

anyways to the point. I don't need anyone to 'fix' anything just need someone to explain something to me.

I've been following an online 'Java For Complete Beginners' and one of the examples we were asked to try is how to set up arrays using the for loop. What I normally do is read the example code, figure out how it is working then code it in my UDI without going back to the example to see if I can figure it out on my own. When I ran my code rather than giving me the numbers 1-49 it gave me 0's instead. Baffled by this I looked over the example given and there was only one thing that was different between the code they had wrote and the one I wrote. They wrote lotto[I] = I+1; and I had written lotto[I] = i++;

The entire code is as follows:

So my question is, why is it that lotto[i] = i++; gave me nothing but zeros. The only thing that comes to mind is that perhaps the i++ doesn't work if assigning the value to another variable, or that perhaps outside of a loop statement it doesn't work. I'm asking because I don't want to assume anything and get into bad habits because of one instant.

Jeff Verdegan
Bartender
Posts: 6109
6
You have to stop and think about what i++ means, and how it's different from i+1.

Add this after your existing loop, and see if it gives you a hint.

Daniel Rich
Greenhorn
Posts: 16

I did as you said and I understand. Each cycle around the loop 1 gets added to the value of i however many times the value of lotto.length.

So, my understanding of i++ is simple short hand for saying add the value 1 to whatever is currently in i. i+1 is doing the same thing adding the value of 1 ontop of whatever the current value is.

This seems to be my pickle. I know that if i++ doesn't work i+1 will, however I don't understand why when it says they are the same thing just short hand. This seems a very small detail, but one thing that has been consistent in everything I've read is that making sure to understand these small things will help later.

Again, thanks so much for your help, really am grateful for the help.
Dan.

Winston Gutkowski
Bartender
Posts: 10527
64
• 1
Daniel Rich wrote:This seems to be my pickle. I know that if i++ doesn't work i+1 will, however I don't understand why when it says they are the same thing just short hand. This seems a very small detail, but one thing that has been consistent in everything I've read is that making sure to understand these small things will help later.

The problem is that they aren't the same; and if you read that somewhere, either:
a) You misinterpreted the sentence.
b) The author deserves to have his nadgers cordwangled.

i++ updates the value of i.
i + 1 does not.

Winston

Red Smith
Ranch Hand
Posts: 136
1
• 1
Daniel Rich wrote:Thanks for the speedy reply.

I did as you said and I understand. Each cycle around the loop 1 gets added to the value of i however many times the value of lotto.length.

So, my understanding of i++ is simple short hand for saying add the value 1 to whatever is currently in i. i+1 is doing the same thing adding the value of 1 ontop of whatever the current value is.

This seems to be my pickle. I know that if i++ doesn't work i+1 will, however I don't understand why when it says they are the same thing just short hand. This seems a very small detail, but one thing that has been consistent in everything I've read is that making sure to understand these small things will help later.

Again, thanks so much for your help, really am grateful for the help.
Dan.

I believe that i++ is shortand for

i = i + 1

not

i + 1

But more correctly, it is short hand for - temporarily store the value of i, then add 1 to i and store the result back in i, then return the pre-increment saved value of i. Or another way to say it could be - return the value of i, then increment the value of i and store it in i.

whereas

++i is shorthand for - add 1 to i and store the result in i, then return the value of i

So if you do
lotto[i] = i + 1 you will loop 48 times since i is not incremented from that statement.

but if you do
lotto[i] = ++i
or
lotto[i] = i++

you will only loop 24 times since i is getting incremented an extra time in each loop iteration from either statement.

The zeroes print out because of the fact that i++ returns a value, then stores a value one higher in i.
So lotto[1] gets a value and lotto[2] (still zero) is printed by the next line;
Then lotto[3] gets a value and lotto[4] (still zero) is printed by the next line.

Jeff Verdegan
Bartender
Posts: 6109
6
Daniel Rich wrote:I don't understand why when it says they are the same thing just short hand.

Whatever says that is wrong.

Jeff Verdegan
Bartender
Posts: 6109
6
• 1
Red Smith wrote:
I believe that i++ is shortand for

i = i + 1

It's not, though the difference between that and what it actually does is not always visible.

But more correctly, it is short hand for - temporarily store the value of i, then add 1 to i and store the result back in i, then return the pre-increment saved value of i.

Correct. Although the use of the word "pre-increment" there may be confusing, since this is called the post-increment operator.

Or another way to say it could be - return the value of i, then increment the value of i and store it in i.

No, that's not the same, and it's not correct. Although I don't think it would ever make a difference in a correctly written program.

++i is shorthand for - add 1 to i and store the result in i, then return the value of i

Yup, as long as we're clear that we're returning the new (incremented) value of i.

Daniel Rich
Greenhorn
Posts: 16
Thanks so much guys for the replies. I stepped away from it for a while, came back and tested a few things. I see now that in my code the lotto[i] = i+1 was just so that the array started at '1' rather than zero. I also figured out that another way of doing that would be been to simply gave i the value of 1 at the start or during the for loop statement. I understood what was happening after the first reply Jeff wrote. However wanted to know why i++ gave Zeros which Red explained.

Thanks so much for explaining, I know this was a really 'noob' question, but it makes perfect sense now. Thanks.

Bear Bibeault
Author and ninkuma
Marshal
Posts: 65223
95
"noob questions" are not only OK, they're encouraged! Welcome to the Ranch!

Paul Witten
Ranch Hand
Posts: 86
Daniel Rich wrote:I know this was a really 'noob' question, but it makes perfect sense now. Thanks.

Did anybody point out that you were just trying to use an incrementer (i++) operator in place of a value (i)? Incrementers have no value. It is deceiving. I've made that mistake.

I'm not sure anybody pointed out that you were attempting to increment i twice for each loop. Since it increments in the loop statement there is no need to increment it a second time in the body of the loop. In fact that just causes you to print, "2, 4, 6 ..." if you are handling the assignment int[i] = i correctly.

Did anybody point out that the zeroes are the result of initializing the array of int? Zero is the default value assigned by the JVM to new int.

Essentially you were making lots of changes to i but never assigning i to int[i].

Jeff Verdegan
Bartender
Posts: 6109
6
Paul Witten wrote:
Did anybody point out that you were just trying to use an incrementer (i++) operator in place of a value (i)? Incrementers have no value.

No, the expressions i++, ++i, i--, and --i each has a value. They are, respectively, the value of i before it gets incremented, the value of i after it gets incremented, the value of i before it gets decremented, and the value of i after it gets decremented. If they didn't have a value, you couldn't do this:

Red Smith
Ranch Hand
Posts: 136
1
Jeff Verdegan wrote:
Red Smith wrote:
I believe that i++ is shortand for

i = i + 1

It's not, though the difference between that and what it actually does is not always visible.

What does it actually do?

Or another way to say it could be - return the value of i, then increment the value of i and store it in i.

No, that's not the same, and it's not correct. Although I don't think it would ever make a difference in a correctly written program.

Paul Witten
Ranch Hand
Posts: 86
Jeff Verdegan wrote:
Paul Witten wrote:
Did anybody point out that you were just trying to use an incrementer (i++) operator in place of a value (i)? Incrementers have no value.

No, the expressions i++, ++i, i--, and --i each has a value. They are, respectively, the value of i before it gets incremented, the value of i after it gets incremented, the value of i before it gets decremented, and the value of i after it gets decremented. If they didn't have a value, you couldn't do this:

You are correct, Jeff. I was erroneously thinking, "has a value vs. returns a value" but there is no real difference because you can in fact assign it.

I had to spend some time re-reading Red's last few lines before I finally got it the real reason for the zeroes. If I may:

Hehe, for those of us who forgot the difference between a cell index and a cell value.

Jeff Verdegan
Bartender
Posts: 6109
6
Red Smith wrote:
Jeff Verdegan wrote:
Red Smith wrote:
I believe that i++ is shortand for

i = i + 1

It's not, though the difference between that and what it actually does is not always visible.

What does it actually do?

As already pointed out, i++ is equivalent to: save the value of i, increment i, return the saved value (the value it had before incrementing).

If you really need more details (you don't), they're in the JLS.

Or another way to say it could be - return the value of i, then increment the value of i and store it in i.

No, that's not the same, and it's not correct. Although I don't think it would ever make a difference in a correctly written program.

Red Smith
Ranch Hand
Posts: 136
1

Or another way to say it could be - return the value of i, then increment the value of i and store it in i.

No, that's not the same, and it's not correct. Although I don't think it would ever make a difference in a correctly written program.

You are a puzzle. Your greater than 4,000 posts seems to point to a person who likes answering Java questions. But from the answers you give, it would appear that you you don't like answering Java questions.

Henry Wong
author
Marshal
Posts: 21504
84
• 1
Red Smith wrote:
You are a puzzle. Your greater than 4,000 posts seems to point to a person who likes answering Java questions. But from the answers you give, it would appear that you you don't like answering Java questions.

I think the issue here is that you are looking for an analogy, a way to remember the behavior -- while everyone here is trying to be exact. If all you need to do is learn something for one class, then okay, maybe the analogy "i++ is shorthand for i = i + 1" is good enough -- specifically for the easy cases that you will encounter.

Go back and reread Jeff's response. It may sound exactly like your statement, but it is not. And IMO, if you can't figure out the subtle difference, I would not worry about it too much. Just remember this topic, and in the future, after you have done more complex coding, revisit this topic.

Anyway... how about some examples? ... especially since these days, young folks really hate waiting for the future...

Is "b++" the same as "b = b + 1" ? Simply, no. If you try it, the first will compile, and the second won't.

How about something more complex? For example, you are incrementing an element of any array?

Is "arr[i]++" the same as "arr[i] = arr[i] + 1"? Maybe.

How about we make it more complex? What is the equivalent of "arr[i++]++"? As you can see, the analogy is starting to break down. And we are no where near complex yet.

EDIT... BTW, in reading this topic over again -- I believe that both Red and Jeff has the same and correct understanding of the issue. The debate is simply over the analogy that was used to explain it.

Henry

Jeff Verdegan
Bartender
Posts: 6109
6
• 2
Red Smith wrote:
You are a puzzle. Your greater than 4,000 posts seems to point to a person who likes answering Java questions. But from the answers you give, it would appear that you you don't like answering Java questions.

I'm here to help people figure out their Java problems. The philosophy I try to follow is to put as much of the burden onto them as possible, and give them just enough information--just a nudge in the right direction--to get them past the hump toward solving their own problems. I expect people to read carefully, to spend time thinking, and to struggle with finding the answer. If I feel they're struggling too much and not making progress, I give them another little nudge. I'm generally not interested in just handing people the answers to their questions, although depending on the nature of the question and my own personal opinion about the individual, I do bend this rule to a large degree sometimes.

I believe this is an effect approach to education. Others may favor different approaches.

Jeff Verdegan
Bartender
Posts: 6109
6
Henry Wong wrote:
EDIT... BTW, in reading this topic over again -- I believe that both Red and Jeff has the same and correct understanding of the issue. The debate is simply over the analogy that was used to explain it.

It is true that I tend to favor precision over simple analogies, although it's not uncommon for me to comment on the breakdown between the analogy presented and the precise rules, and to give some idea of when the simpler but less precise view is good enough.

I think it's better to learn the precise rule the hard way now than to learn "close enough" now and then have to unlearn it later.

Winston Gutkowski
Bartender
Posts: 10527
64
Paul Witten wrote:If I may:...

Paul.
Please don't put enormously long lines inside code tags. They screw up the windowing and make threads difficult to read (besides being bad coding practise). I've broken yours up this time.

Thanks.

Winston

Paul Witten
Ranch Hand
Posts: 86
Winston Gutkowski wrote:
Paul.
Please don't put enormously long lines inside code tags.

Got it, didn't realize they were that long.

Paul Clapham
Sheriff
Posts: 21416
33
Jeff Verdegan wrote:I think it's better to learn the precise rule the hard way now than to learn "close enough" now and then have to unlearn it later.

Here's an alternate approach, the one I use: Don't ever use the value of an expression like i++. If you want to increment i, then sure, put that expression into your code, but don't assign its value to any variable or use it in any computation.

This greatly simplifies things, because now all I have to learn is that i++ and ++i increment the value of i.

And if I ever encounter the situation where I have to work with somebody else's code, and they didn't follow this rule, then, well, I know where to go to find an explanation of what's going on.