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

q from sun's epractice exams

 
Ranch Hand
Posts: 303
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


i dont know the ans to this q.plz help.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What answer does it give when you run it? Is there a particular expression or operator you do not understand? You can usually solve these problems by using a table with variables and their values. And, of course, putting in appropriate println statements at various interesting points in the computation.
[ December 26, 2004: Message edited by: Barry Gaunt ]
 
JayaSiji Gopal
Ranch Hand
Posts: 303
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am confused. at no point of time, is the condition in the if loop going to be satisfied.so the value of t=0. but the answer options dont reflect the same.

plz help.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The if condition is satisfied when t is 3 and m is 2. Do not forget that there is a t++ in the if that modifies t every time it is tested.
[ December 26, 2004: Message edited by: Barry Gaunt ]
 
JayaSiji Gopal
Ranch Hand
Posts: 303
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if(t++ > m)
is equivalent to if(t > m){ t++; //rest of the code }

am i right? if so, how is the value of t getting incremented?
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No you are not correct. Check out your BASIC Java book on how an expression like "t++ > m" is evaluated. Or write a simple piece of code to print the values of "t++ > m" and t.
[ December 26, 2004: Message edited by: Barry Gaunt ]
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Barry Gaunt:
No you are not correct. Check out your BASIC Java book on how an expression like "t++ > m" is evaluated. Or write a simple piece of code to print the values of "t++ > m" and t.

[ December 26, 2004: Message edited by: Barry Gaunt ]



Barry,

Why it's not correct?
I think what JayaSiji is probably correct, as the postfix ++ operator in this case is evaluated after the comparsion, and the original value for t is used instead.

So, for an example :


This piece of code will evaluate to true, as the value 0 of xxx is used in the comparison, and when the print statement executes, it will print :
Equals 1 : 0

Any thoughts?
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"if(t > m){ t++; //rest of the code }" means that t will only be incremented if t is greater than m. That is not the same as "if (t++ > m) {//rest of the code }" in which t will be incremented regardless of the relationship between t and m.
[ December 26, 2004: Message edited by: Barry Gaunt ]
 
Ramy M. Kamel
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Barry Gaunt:
"if(t > m){ t++; //rest of the code }" means that t will only be incremented if t is greater than m. That is not the same as "if (t++ > m) {//rest of the code }" in which t will be incremented regardless of the relationship between t and m.

[ December 26, 2004: Message edited by: Barry Gaunt ]



Barry, you are correct, I didn't see it from this prespective.

I just wanted to show that the result of the ++ operator is not used in the comparison itself.
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think Barry's basic point is that JayaSiji should start compiling and executing these programs himself. He can even try changing the programs to see how the results change.

There is a basic principle of education that the more senses you involve in studying, the better you learn. This is strongly backed by recent research in brain functionality.

So, JayaSiji, please run some of these programs yourself. If you have difficulty setting up the Java SDK, just ask us for help.
 
Our first order of business must be this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic