34

# *** don't use compiler *** Question in class today: int n = 0; for (int i = 1; i < 10; i++) { n = n++; System.out.println(n); } what will be printed?

• 15
1
2
3
4
5
6
7
8
9
• 4
@calmyourtities thank god I'm not the only one
• 2
What @calmyourtities said
• 9
It prints 10 times "0"?
Because if you use n++ it returns the n value and not the n+1 value...
• 12
0
0
0
0
0
0
0
0
0
• 3
Never increment and assign on the same line in production.
• 7
@sslPoodle the compiler will do it in two parts.
n = n++; -> n = n; n++; -> n = 0; 0++;
Ergo, N will increment anyway.
• 1
if you have ++ after, it will increment after returning it’s value, so it will do:

n = current n

Then:

n += 1

So n=n++ is the same as n++
• 10
Seriously, who writes shit like this?
• 2
@Root he asked if class knew exactly how the increment operator worked. Everyone was like "yeah ofc, how hard is it?" then he showed us this and there was like 1/100 who knew.
• 2
Oh FOR FUCKS SAKE GET RID OF THE GODDAMN LOOP IF ITS NOT BEING FUCKING USED.

Wow, Java devs really are efficient. I think I'll forward this to my prof...
• 4
The postfix operator ++ has the higher precedence so it is evaluated first. It increments n but returns the old value, then the = operator assigns the result of the ++ operator (the old value of n) to n. So n is not incremented!
• 1
n
• 1
Break it up into two parts. First the assignment operator resolves setting n to itself. Then n increments itself.
• 0
@mgagemorgan how would you print 9 times 0 without a loop then?
• 2
@mgagemorgan This has nothing to do with Java or efficiency.
1) It's the same in every language.
2) It's to make people think about how code works. This does not have to be efficient or logical in any way.
• 1
@Forside Meh. I dont do java because it uses a vm - also dont make me think if its not logical. I'm not arguing any further.
• 4
n++ means afaik n = n + 1
Therefore, I would assume that n = n++ would mean n = n = + 1
But having two equation symbols might break it. Idk. Barely have to do with Java lmao
Besides of that thing. If it would be a regular n=n+1 the result would be:
1
2
3
4
5
6
7
8
9
• 1
A whole bunch o' zeroes
• 0
Something about the way int n is declared outside the loop looks wrong to me. I dont know what, it just looks wrong. Maybe it's because for anything more than i I just use a while() loop for whatever it is I'm doing. Been awhile since I've had the time to fuck around with constructs...actually ~6 months thanks to coursework.
• 1
@gitpull `n++` returns the value of `n` and then increments it; two very separate operations. However, the `n=` portion adds a third.

The question thats actually being asked here is which one happens first; specifically, if the assignment portion of `++` has greater or less precedence than `=`.
• 1
@Root I get your point, but would not 'n=' just assign the value of 'n++' to itself again. Like... you have the following.:
int n = 0;
n = n + 1; //n = 1
total = n; //still 1 until the next iteration
• 1
Wait... I think.. I am just confused haha
• 1
Isn't my example just the same as what you did explain
• 1
After running it I can safely say, it just prints a bunch of 0's. Which means that it first returns, then it increments
• 0
@gitpull
n=0 // initial value

// n = n++ could mean...

/* possibility 0 */
temp = n // n++ return portion
n += 1 // n++ assignment portion
n = temp // n=
// result: n=0

/* possibility 1 */
temp = n // n++ return portion
n = temp // n=
n += 1 // n++ assignment portion
// result: n=1

(Because I'm unfamiliar with Java and lack access to it presently, I don't know which one actually happens.)
• 2
@gitpull Same. It's one of those example problems thats equivalent to the combination of "Ha! Made you look!" shit siblings do to each other and those workshits teachers give to see if you can follow directions. RIP critical thinking!

I dislike this because of the use of a separate variable used with a for() loop as an incrementor. I don't recall ever being in a situation where I needed a second variable, which may be why I hate this so much.

I have a headache and am probably overthinking this...
• 2
@Root
Lemme test that in C in 20 minutes :)
• 1
@Root I've never used it, and don't plan to outside of required courses. I know the Oracle fuckboys over in IT are gonna wanna rip my guts out because I just talked shit about the only language they seem to know, buuuut..
• 1
@gitpull I'm curious if the behavior differs between Java and C!
• 1
@Root - I'm gonna laugh if the results are different. Then again, something as simple as structs seem to change constantly between languages. What's in C is NOT in C#.
• 1
@mgagemorgan Absolutely. And after seeing so many things like this, I'm rarely surprised anymore ☹
• 0
@loin I dont know why I said for loop. I meant "n". Today has been a mess.

The real question is why the actual fuck I would want to print zero nine times in the first place...
• 4
@gitpull we need answers. Does C and Java behave the same and what is the output of the C program? (Laying in bed and am to lazy to get back up :P)
• 4
.
• 2
@Root it's the possibility 0
• 2
@Wack Done
• 1
@gitpull Same for Java. Are we all wrong?
• 1
Seems like it :)
Btw. the Running and Done thing in the terminal... it looks cool
• 1
@Forside Well, fuck me because I'm an idiot and (to quote @Root) who writes shit like this anyway?
• 3
Thinking about it, I actually "lerned" that in my first semester too.

If it would be `n = ++n` it should be as "expected" though.
• 1
500 ✌️
• 3
@mgagemorgan not anymore :P
• 2
@gitpull What I expected 😊

@Forside totally glad it's the same in Java haha

@Wack yep yep.

@mgagemorgan Seriously. This crap is only ever seen on college exams and within interns' code. And there's probably a strong correlation between the two... 😞😡
• 1
@gitpull It's the ouput of the VS Code Code Runner plugin ^^

@mgagemorgan I personally like experiments like these. Scrolling through the thread I just recognized the post by @electronix. Knowing that, it makes sense, but I also thought ++ would be executed after =.
• 2
@Root Yeah, that's where my bias is. I cannot defend the stupidity of the code nor the predictability of a college prof for this.

"It makes you think" is a bullshit excuse - yeah, but THIS fails to prove anything. Other than the fact that it's horseshit...
• 2
Just realizing how stupid I am.
If you ever learned how operators are defined in C, you know that it behaves like this:

n = n++
n = {
int tmp = n;
n++;
return tmp;
}

So yeah, in the end it's n=n.
• 1
@calmyourtities it's staggering that you get so many upvotes for your incorrect answer. Maybe it's because you answered first? Maybe it's just me not getting the joke of answering incorrectly, but probably the worst case scenario is true: most people just don't understand the code.

Everybody should know about the ++n and n++ expressions and what their difference is. If you'd have known that, then the only logical explanation is that if n = ++n is something different than n = n++, then the result of n = n++ would not be that n is changed.

The most important thing to remember is that assignment (n = ...) happens last: after the right part of the equation has been evaluated. The right part returns 'n before incrementing' by definition of the n++ operation. I don't care if n is set a million times on the right hand side, just that the right side returns the original n's value.
• 0
@eeee It's the latter. And it's also not something I'd ever write personally in any of my projects.
• 3
#wk88 for y'all. Except @eeee
• 0
@eeee lol man i know i saw that after the first comment that said that but it's free ++