• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Please help

 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why the following programme was showing me the output 5
as far as the rules of pre increment, the value is incremented before being assigned and in post increment, the value is assigned to variable before being used. So why the output was 5 not 6?
Another agony,
Why the following programme compiling successfully?
Please help with suitable reason. Thanks in advance for help.
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jack back:
Why the following programme was showing me the output 5
as far as the rules of pre increment, the value is incremented before being assigned and in post increment, the value is assigned to variable before being used.



The post increment happens AFTER evaluating the value of the variable, and BEFORE assignment to the left hand side. The order is this:
a = 1;
EVAL:
Right Side = a = 1; [from a+=]
++a -> 1+a -> 2; [from ++a]
Right Side = 1 + 2; [from + ++a]
a++ -> a -> 2; [from a++]
Right Side = 1 + 2 + 2; [from + a++]
a++ -> a + 1 -> 3; [from a++]
Right Side = 5; [from a+= ++a + a++]
ASSIGN:
a = Right Side = 5;

So why the output was 5 not 6?
Another agony,
Why the following programme compiling successfully?



Because you are allowed to have public static inner classes inside an interface...
 
Sheriff
Posts: 22862
132
Eclipse IDE Spring TypeScript Quarkus Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Jack back",

Please read your private messages regarding an important announcement.

Thank you,

Rob
 
Marshal
Posts: 80962
525
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
. . . and, in future, please use a title which tells us what the thread is about; "Please Help" isn't very useful.
 
Pawan Arora
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Steve, thanks for your reply.
I still have one problem, why the a+= is not like a=a+1??
[ October 12, 2008: Message edited by: Jack Black ]
 
Sheriff
Posts: 67756
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Jack Black", did you check your private messages as Rob requested? Please check again.
 
Ranch Hand
Posts: 1179
Mac OS X Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jack Black:
Thanks Steve, thanks for your reply.
I still have one problem, why the a+= is not like a=a+1??



a += means a = a + x
 
Rob Spoor
Sheriff
Posts: 22862
132
Eclipse IDE Spring TypeScript Quarkus Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Bear Bibeault:
"Jack Black", did you check your private messages as Rob requested? Please check again.


Indeed. Unless you are a short pudgy actor / singer, either your name is not allowed or you need to show us that Jack Black really is your name.
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No offense, but this sounds like a homework question... am I the only one thinking this?
 
Steve Luke
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jacob Steingart:
No offense, but this sounds like a homework question... am I the only one thinking this?



I think it is a question from homework - yes, but he isn't asking to have it done for him. He is asking for understanding the output, which I think is appropriate.

If it isn't homework (or studying for some test) then the correct answer is: Don't do that!
 
I'm doing laundry! Look how clean this tiny ad is:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic