| 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: 161
|
|
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: 14685
|
|
|
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: 32833
|
|
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!
|
 |
 |
|
|
subject: Loops question
|
|
|