This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Loops question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Loops question" Watch "Loops question" New topic
Author

Loops question

Rob M Jones
Greenhorn

Joined: Nov 16, 2011
Posts: 2
Hi everyone, I am currently studying java and came across this question on one of the practice exams. I have stepped through each iteration of the loops but am stumped as to why x is never increased to 4 when i is set to 1. Can anyone shed some light on this?
Thanks!



By the way the output is --,<
amro talaat
Greenhorn

Joined: Sep 02, 2012
Posts: 4
Rob M Jones wrote:Hi everyone, I am currently studying java and came across this question on one of the practice exams. I have stepped through each iteration of the loops but am stumped as to why x is never increased to 4 when i is set to 1. Can anyone shed some light on this?
Thanks!



By the way the output is --,<


try if else ???
Carey Brown
Ranch Hand

Joined: Nov 19, 2001
Posts: 173

Rob M Jones wrote:Hi everyone, I am currently studying java and came across this question on one of the practice exams. I have stepped through each iteration of the loops but am stumped as to why x is never increased to 4 when i is set to 1. Can anyone shed some light on this?
Thanks!



By the way the output is --,<


Try
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Check this FAQ.


[My Blog]
All roads lead to JavaRanch
Ankit Dan
Ranch Hand

Joined: Aug 31, 2012
Posts: 47

why x is never increased to 4 when i is set to 1.


you can see that you have assigned the value of post increment x to x i.e x=x++
so x is assigned a value before it is incremented
you can try x = ++x
or simply x++ or ++x

hope it helps


Ankit
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38044
    
  22
The idea of that question may be to find out whether you can see
i = i++;
and realise there is a trap for the unwary!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Loops question
 
Similar Threads
Really beginner question
I thought I understood this stuff.....
While loop - Head's First Java example
Dans Questions : logic
Need Help With For Loops