Win a copy of TDD for a Shopping Website LiveProject this week in the Testing 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Jtips #29

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you please tell me why the value of j is 7?
Thanks, Yuki

class Q29 {
public static void main(String[] args) {
int j = 0;
Q29 test = new Q29();
try {
for (j=1 ; j < 5 ; j++)
{
j+= j;
}
int i = test.process() / (j = 5);
}
catch (Exception e) {
System.out.println(e);
System.out.println("Value of j = " + j);
}
}

int process() throws Exception {
throw new Exception("Exception Encountered");
}
}
Answer: Prints java.lang.Exception: Exception Encountered and Value of j=7.
 
Ranch Hand
Posts: 400
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi...,
let examine the "for looping" :
1. syntax is :
for (expression1, expression2, expression3)
{
...//loop body
}
2. expression3 will be evaluated if and only if the the expression2 is "true", and the loop body is complete normally (i.e - didn't use "break" to stop the looping)
let see your code :
for (j=1 ; j < 5 ; j++)
{
j+= j;
}
1. when j = 1, expression2 is "true", the loop body will be evaluated j = 1 + 1, j = 2,
2. and then evaluated expression3 (j++), j now is 3,
3. when j = 3, expression2 is "true", the loop body will be evaluated j = 3 + 3, j = 6,
4. and then evaluated expression3 (j++), j now is 7,
5. when j = 7, expression2 is "false", the loop body will not be evaluated (last value of j = 7)
now let see second code :
1. if LHO is "complete abruptly", the RHO will not be evaluated
int i = test.process() / (j = 5);
}
since LHO (test.process) complete abruptly, RHO (j = 5) will not be evaluated, so j is still 7.

hope thats help
stevie
 
Yuki Cho
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That really helps. Thanks Stevie!!!
-Yuki
 
I’m tired of walking, and will rest for a minute and grow some wheels. This is the promise of this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic