• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Bear Bibeault
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • salvin francis
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
Bartenders:
  • Jj Roberts
  • Carey Brown
  • Scott Selikoff

post increment & assignment operator

 
Ranch Hand
Posts: 96
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I totally understand this code.

Line #3
step 1: returns the original value of car (1) and assigns this value 1 to the variable van.
step 2: increment car by 1.
So car's value is now 2.
And van's value is 1.


----------------------------------------------------------------
But the following code confuses me.

Line #2

step 1: returns the original value of car (1) and assigns this value 1 to the variable car.
step 2: increment car by 1.
So car's value is now 2.
But Java shows that the car value is 1.


 
Bartender
Posts: 7804
70
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
car = car++;
Take the car value BEFORE incrementing it and remember it
increment car
assign to car the remembered value it had BEFORE incrementing
 
henry leu
Ranch Hand
Posts: 96
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I understand the assignment step. The car value before the incrementation is 1. This one is assigned to the car.
But what happen later to the increment step? Where does this incremented value go? Doesn't car need to increment itself?
 
henry leu
Ranch Hand
Posts: 96
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:car = car++;
Take the car value BEFORE incrementing it and remember it
increment car
assign to car the remembered value it had BEFORE incrementing



I got your point. Thanks!

step 1: Take the car value BEFORE incrementing it and remember it
Step 2: increment car. (THIS VALUE IS GARBAGE. NEVER GOING TO USE).
Step 3: assign to car the remembered value it had BEFORE incrementing

 
Marshal
Posts: 26387
81
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm sure we have an FAQ page for this question, it's one of the most frequent questions asked on this forum. But anyway:



1. Remember the value of car (it's 1).

2. Increment car (now it's 2).

3. Assign the remembered value to car (remember, it was 1).

Clearer now? Try



to make sure you've got it.
 
Carey Brown
Bartender
Posts: 7804
70
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Yes, before this assignment takes place car has been incremented, but the assignment then replaces that with the remembered_car.

If you had
output:
tmp=5   car=6
but in your example, even though car is incremented, we overwrite car with the remembered_car, thus losing the increment.
 
Paul Clapham
Marshal
Posts: 26387
81
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In my opinion they should change the ++ and -- operators so that applying them to a numeric value doesn't produce a value which can be used. Make them like statements, in other words. Sure it would break thousands of programs but it would save millions of hours of people's time dealing with this issue. And anyway people who wrote "val = car++" were committing an act of very bad judgement, in that future maintainers have to look at it and think "Wait, how does this ++ operator work again?"
 
Carey Brown
Bartender
Posts: 7804
70
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which is exactly why, even though there are lots of things you could do with ++/--, it doesn't mean you should. If it doesn't improve the readability of your code it almost assuredly falls into the "don't" category.
 
Rancher
Posts: 941
23
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hernry,

i hope you realize this:

is going to print 2

it is actually incrementing that value contained in the variable a, and not the value that still need be assigned.
 
lowercase baba
Posts: 12965
66
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:I'm sure we have an FAQ page for this question


Here
 
Ranch Foreman
Posts: 235
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And this is (probably) why Python chose to omit both ++x, x++, --x and x-- from its operator set....

There are still plenty of tricky things in Python of course, including the fact that the assignment operators aren't considered to be operators, but delimiters.

The Faq is good, also, there is that coding standard for Java somewhere on this site which basically says "Only use x++, and never in a context where it would have mattered if you had said ++x".

I spent so much time in C/C++ that would be a big ask for me, yet somehow I do without it in Python -- yeah, because it isn't available.
 
Saloon Keeper
Posts: 23282
158
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"car = car++" is confusing, but it might be less confusing if you thought "vehicle = car++".

The compacted operators (+=, <<=, ++, -- and so forth) are artefacts of the C programming language. Or actually, I think Algol, even. The developers of C were A) tired of long typing sessions originally in assembly language and B) trying to make it easier for the compiler to optimise certain operations. It wasn't incommon to have an "increment memory" operation for example (still isn't, I think), which meant that the compiler could see ++ and emit an "INCM" instruction instead of having to load an accumulator, add a constant 1 to it and store it again. And a common construct is "while (i-- > 0) ...".

The exact reasoning why Guido Rossum didn't support such constructs is unknown to me, but Python is visibly designed to be less cryptic than the C/Algol languages and it's less "mathematical" and modern compilers generally do pretty extensive optimisation.
 
Jesse Silverman
Ranch Foreman
Posts: 235
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am old enough to remember, and what's more, it made an impression on me at the time, that the late Edsger Dijkstra and Niklaus Wirth absolutely DETESTED many of the short-cuts that made it into expressions in C, in a big way.  I just searched for quotes from them and found lots of great stuff, but not that.  Bertrand Meyer has some great ones too...

To me as a kid, they were computer science, so that I was in love with a language that did things they hated felt weird.

Since then, almost everything they complained (in terms of expressions at least) about has been propagated in C++, Java, Perl, and most of the others...also, just the level of complexity of the languages...

Most of the code I worked on at my second to longest job had:
( CONSTANT == var) and even ( CONSTANT != var) everywhere to prevent the errors that used to creep in before linting/static analysis by accidentally writing the syntactically kosher while  ( var = CONSTANT )

I don't know how much := would have helped there....and FORTRAN committed the Original Sin, not C....

Anyway, AFAIK yet, Python follows most of these C "innovations", the interesting omissions are ++/-- and exclusive use of "and" and "or" rather than '&&' and '||' for the short-circuit logical operators and handling all of the '=' variants as delimiters, rather than operators.  Oh yeah, and that you can write if ( a < b > c <= d ) and such...

Personally, I never had a problem with ++ and -- except that having written very low-level stuff and done operator overloading in C++, I really, really, thought all the standalone uses should always have been written ++x or --y.  I think a lot of the confusion surrounding them comes from people getting so used to writing those the wrong way when it doesn't matter.

I have noted that this site has a Style-guide somewhere that says "Always write x++ and never use it where it makes a difference..."
That doesn't help for certification exam seekers or those who work on teams (like most?) who love them...
 
Tim Holloway
Saloon Keeper
Posts: 23282
158
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
People who think about programming in the abstract (Djikstra, for example) and people who actually have to type code all day long have very different perspectives. As do people who want efficient code in an era where optimising compilers were extra-cost options and you sometimes had to give the compiler a little nudge.

Actually, if you want real abstract, look at Kenneth Iverson's APL (literally, "A Programming Language"). APL requires a special keyboard and output devices. It's a mathematical-like symbolic notation specially designed for the analysis and processing of array operations. It's available for the Linux OS. In fact, it's installed on my machine.

FORTRAN was designed in 1954 and the IBM keypunches of the day didn't have a colon key. In fact, I think you had to backspace-and-overpunch just to get "=". Plus it was designed for mathematicians, so making an assignment statement look like an expression of equality probably made sense to them. Algol, of course, knew better. But I don't know what data entry equipment for Europeans looked like back then. Algol is rather famous for using one sort of notation in texts (for example, boldfacing keywords) and another notation where you actually had to produce something machine-readable.
 
Marshal
Posts: 72088
312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesse Silverman wrote:. . . while  ( var = CONSTANT )

That will easily make it past the compiler in C and C++ but javac will complain bitterly about it, unless the variables are of type boolean. That has to do with using (or not using) 0 for false and non‑0 or non‑null for true.

I don't know how much := would have helped there . . .

It would have made the difference that matters. Because it looks different from the traditional equals sign, it would have made the mistake obvious.

. . . this site has a Style-guide somewhere . . . .

Here. Scroll down to §3.2. We don't all believe those suggestions. The only places I would use compounded operators are in something like an array list or an array stack:-See Effective Java by Joshua Bloch, the section about avoiding retaining excess references and causing memory leaks. Compounded ++ and -- operators do cause confusion (see the lion and tiger example from Boyarsky and Selikoff's OCJP revision book), but the idiom values[size++] is common enough that developers need to become familiar with it.
 
Campbell Ritchie
Marshal
Posts: 72088
312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:. . . very different perspectives. . . .

Another different perspective comes from the size of the machine. If you spent thousands of £$ (€ hadn't been invented yet) on memory, and it was still limited in capacity, or your screen struggled to show more than 25 lines at once, every keystroke was expensive, and the old C books showed all sorts of abbreviations. Not only compounded ++ but squeezing the entire logic into the header of a for, so it was respectable to write for(...;...;...);
Now that you can buy screens the size of dinner tables, and you can buy enough memory to store the names of everybody on earth for less than it costs to take the family out to dinner to celebrate your birthday, such abbreviations are no longer really necessary.
 
Tim Holloway
Saloon Keeper
Posts: 23282
158
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think that screen size was a determinant. Back then we were still using punched cards and happily using 72 columns or less of an 80-column card. And some IBM terminals were only 12 lines x 40 characters, but thankfully none I ever had to deal with.

It was a pain to be limited to 6 characters for variable names, though. Mostly for external names, though, since the style of the times was to write "mathematically" - i, j, k, f, g, a, b, c. Only in COBOL did things get verbose. It wasn't really symbol name storage that cramped most stuff, since compilers would immediately translate the names to internal symbol IDs of 1 storage word or less. The killer was all that processing logic, which is why multi-pass compilers were the rule of the day. In fact, part of the problem getting AT&T's C++ onto IBM PC's was that unlike the horizontally-layered multi-pass model of older compilers, the program flow in their C++ translator ran vertically, and IBM-family PCs were still working with 64 kb segments.

Linus Torvalds has famously declared that if you have a monitor screen the size of a dinner table, you should use all of it for code. I've expressed a contrary opinion elsewhere on the Ranch.



Fo my birthday, I've considered handing out Raspberry Pi CPUs as party favours. With 1GB of RAM each.
 
Carey Brown
Bartender
Posts: 7804
70
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Way back when (?) I had been reading about C++ in Dr. Dobbs magazine and got very excited about it. But when I started calling around to get a copy I was just met with blank stares. I put in several calls to AT&T's research branch till I finally found someone who knew what I was talking about. He said, "yeah, we have it be we can't sell or give it to you. But, you're in luck because just this week the first commercial copies are being sold through a company called Glockenspiel out of (some country I can't remember)." So after making my first trans-Atlantic call I was the proud owner (well, my company was) of one of the first commercially available copies of C++. And at that time it was not a true compiler, it was a CFRONT compiler which translated C++ to C first and then compiled the C. I was running this on a Sun workstation to do image processing.

Getting in on the ground floor had advantages and disadvantages. Being able to look at the intermediate C code gave me a better understanding of what the C++ syntax was doing for me; some of which included generating pages of unnecessary C code. I learned to avoid those constructs. But the bleeding edge of CFRONT was sharp. Every two months the new release would require me to go through all my code and re-write parts of it to follow the new syntax.

My real object-oriented ah-ha moment came when I was working on a Fast Fourier Transform, which was working just fine, but it calculated all the frequencies in my 2D image and I only needed 2 of the frequencies. My math background is/was not strong enough to help me re-engineer the algorithm for that, so instead, I made a symbolic math class that instead of performing the various adds, multiplies, etc., I overloaded the operators to build a parse tree as the operations were applied. Then, because the frequencies ended up in an array, I only needed the parse trees from two of the frequency elements. I sprinkled a little be of parse tree optimization: tree pruning anything multiplied by zero, and node shifting if multiplied by one. And then code to generate C code from the parse tree and voila, exactly what I needed, and it ran as quick as heck.

Sigh. Miss those days.
 
Tim Holloway
Saloon Keeper
Posts: 23282
158
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I had the jump on you. I first heard about it from Bjarne Stroustrup's article in the ACM SIGPlan issue somewhere about September 1986. It included contact information at AT&T Bell Labs, and for about $5K I got a copy of the translator and VAR rights. A friend worked at the local AT&T office and was able to offload the magtape image into a 3B3 computer and we then transferred it to the Amiga. It was a fairly straight port, since the Amiga was a 32-bit machine with a flat memory model just like AT&T's machines.

Not too long after that, a fellow named Comeau came into competition with me and we had some interesting conversations. Since I was under-capitalized, I wasn't able to upgrade to later translator versions and, as previously mentionted dead-ended in doing a native compuiler, so I let the business slide and ceded the market to Comeau.

I also made my first trans-oceanic phone call to Glockenspiel in Ireland about a year or 2 later when I got tapped to do C++ under an early release of Microsoft Windows. Although, thanks to quirks in the telephone tariffs of the day, I'd been regularly dialing up BBS's in Simi Valley CA, and Shawnee Mission, KS, since it was cheaper than in-state calls, even when the destination was just across the river from me.
 
Carey Brown
Bartender
Posts: 7804
70
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote: A friend worked at the local AT&T office...

It pays to know people in the right places.  ;)
 
Jesse Silverman
Ranch Foreman
Posts: 235
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You remind me of something I typed in an e-mail with a dozen CC's back in the early mid-90's:

"What the hell is a Glockenspiel"?

We didn't use C++ (much) at Information Builders, but we had over 20 ports of Focus, and I built, debugged and worked on almost all of them -- somehow Glockenspiel was on one of my to-do lists....

They just had an absolute Alderaan moment over there (IBI, not Glockenspiel), I think I lost 300 to 500 years of colleague experience employment there today in one fell swoop, perhaps more.

Many of them had been there for years when I joined them at the beginning of 1991 as a wet-behind the ears kid who loved his Atari ST's but had also worked in FORTRAN, PL/I and C and had written a single-user DBMS that implemented the full relational algebra for my senior project....they had survived 20+ rounds of RIF's....

it is a Black Day for many people who somehow went 30 to 40+ years in a monogamous relationship with one (very popular) product line across all significant sectors of commercial computing (and a lot that had once been significant)....
 
Jesse Silverman
Ranch Foreman
Posts: 235
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I was in charge of getting our Unix-only build/deploy system running on Windows, I was totally chuffed to often get e-mails and sometimes calls from David Korn (working on bug reports I made to KSH), because I selected UWin as our compatibility layer so Windows could look like just another *nix for all our Unix-y people..

I am glad that I disregarded the people who told me to just wait for Windows Server 5.0 (later renamed 2000) because "it was going to have Unix Compatibility"...yeah, right.
 
Jesse Silverman
Ranch Foreman
Posts: 235
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
and Tim reminds me of when in the early mid-90's I accidentally racked up $300 to $500 worth of phone calls to a girl I was sorta dating in New Jersey.
I made tons of calls to California that never made a dent in my bill, so I was gob-smacked to see dozens of times higher fees/tariffs calling someplace I could get to in a 35-minute bus ride.
Doh!!
It might have literally been cheaper to go see her in person....good times!
 
Paul Clapham
Marshal
Posts: 26387
81
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Back in the 1980's I was working at a project to work with school budgets, and I was writing the code in APL. (Really.) This involved connecting to a server three time zones away and working remotely all day long. In those days long-distance calls were ruinously expensive, but the calls were billed based on the time the call started and there was a significant discount for calls between midnight and 8 am. I seem to recall it was a 60% discount or thereabouts, and the full rate was in the same range as the amount I was getting paid to do the work (on an hourly basis).

So my practice was to arrive at the office early and connect to the server at 7:55 am. The discount rate lasted all day -- except when the line dropped and I had to reconnect. Fortunately that didn't happen too often.
 
Jesse Silverman
Ranch Foreman
Posts: 235
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I never used APL but worked closely with a mathematician who had done a lot of APL on a huge C/C++ application.

I regularly fixed memory leaks (and worse) in his code, but I learned *so* much from him including a proper understanding of the Monty Hall problem.

When he would free a pointer he would write:



He got high blood-pressure working on all the C code and went back to being a math professor afterwards.  He was much happier.

I thought that the modern continuation / spiritual successor of APL was J:
https://en.wikipedia.org/wiki/J_(programming_language)

Anyway, yeah, Unix and C were created by people who had very small memory spaces and drives and also hated typing and were bad at it, hence /usr instead of /user

Reading PEP 808 to see how to write Python code people won't hate reading, I see a lot of things that had bled over into C/C++/Java/C# projects I worked on.

The relative importance of readability is higher than ever, except in a very few places where it isn't.

Thanks for the memories.
 
Tim Holloway
Saloon Keeper
Posts: 23282
158
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I was in school, I created an "APL" system for the lab minicomputer. Had to get creative because I didn't have either input or output devices that could handle the actual APL character set, but it was fun.

Maybe it's because I'm extremely paranoid, but I've almost never had memory-leak problems in C/C++. Modern C++, of course has mechanisms to reduce that possibility, and Rust takes it one step further, but even back in ancient times, I rarely had memory issues.
 
moose poop looks like football shaped elk poop. About the size of this tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic