• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

increasing and decreasing in loop

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want someone to explain when to use ++x, --x, ++y, --y in for loop with examples and codes.

Secondly, look at this code :

class Uber{
static int y = 2;
Uber(int x) {this(); y = y*2;}
Uber() {y++; }
}
class Minor extends Uber{
Minor() {super(y); y = y+3; }
public static void main (String[] args) {
new Minor();
System.out.println(y);
}}
The answer given in the book of Kathy and Bert is 9.But, i think the answer should be 7. Because,y = 2 and y*2 is 4. With increment of 3 plus 4 i.e y+3(3+4) =7
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Lao Kinsuyi:
...The answer given in the book of Kathy and Bert is 9.But, i think the answer should be 7. Because,y = 2...


But the first line of Uber(int) is this(), which calls Uber() to increment y to 3.

Try adding println statements to see what's happening...
 
Maybe he went home and went to bed. And took this tiny ad with him:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic