This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Increment operator

 
ahsan mir
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello,
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
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it!
the x++ only comes into play once x value reaches 3 in the third iteration.
 
wise owen
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the x++ only comes into play when x value greater than 2 which is from the third iteration to end.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic