Help coderanch get a
new server
by contributing to the fundraiser
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
• Jeanne Boyarsky
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Rob Spoor
• Devaka Cooray
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Carey Brown
• Frits Walraven
• Tim Moores
Bartenders:
• Mikalai Zaikin

# q from sun's epractice exams

Ranch Hand
Posts: 303
• Number of slices to send:
Optional 'thank-you' note:

i dont know the ans to this q.plz help.

Ranch Hand
Posts: 7729
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:

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
• Number of slices to send:
Optional 'thank-you' note:
"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
• Number of slices to send:
Optional 'thank-you' note:

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
• Number of slices to send:
Optional 'thank-you' note:
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.

 Doe, a deer, a female deer. Ray, a pockeful of sun. Me, a name, I call my tiny ad ... We need your help - Coderanch server fundraiser https://coderanch.com/t/782867/Coderanch-server-fundraiser