This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Increment operator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Increment operator" Watch "Increment operator" New topic

Increment operator

ahsan mir
Ranch Hand

Joined: Jul 17, 2006
Posts: 42
i dont get why the output is x=8 , y=2 and NOT x=4 , y=1.
A step by step solution would be appreciated


public class Foozit {

public static void main(String[] args) {

Integer x=0;
Integer y=0;
for(Short z=0;z<5;z++)
if((++x >2)||(++y > 2))
x++;//in the second iteration x starts at 1 and not 2?!
System.out.print(x + " " +y);


ahsan mir
Ranch Hand

Joined: Jul 17, 2006
Posts: 42
Got it!
the x++ only comes into play once x value reaches 3 in the third iteration.
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
the x++ only comes into play when x value greater than 2 which is from the third iteration to end.
I agree. Here's the link:
subject: Increment operator
Similar Threads
Looping constructs
increment doubt
Strange IF condition
|| operand