• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Loops question

 
Rob M Jones
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 1685
22
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check this FAQ.
 
Ankit Dan
Ranch Hand
Posts: 47
Eclipse IDE Java MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50189
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic