Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Loops question

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 --,<
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ???
 
Saloon Keeper
Posts: 9574
79
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check this FAQ.
 
Ranch Hand
Posts: 47
MyEclipse IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 76492
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Oh sure, it's a tiny ad, but under the right circumstances, it gets bigger.
The trailboss has a kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic