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

# increasing and decreasing in loop

Ranch Hand
Posts: 30
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:

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...