• 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
  • 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

Sun ePractice question 5 loop/post-increment

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
https://tmn.sun.com/WLC/GuestLogin?cmd=login1&loginType=1&language=en&id=programmer0000
Question 5
1. public class TeSet {
2. public static void main(String args[]) {
3. int m = 2;
4. int p = 1;
5. int t = 0;
6. for(;p < 5;p++) {<br /> 7. if(t++ > m) {
8. m = p + t;
9. }
10. }
11. System.out.println("t equals " + t);
12. }
13. }
Please tell me where the below logic is wrong. Slashes denote an old value is wiped out and the latest value takes its place:
m=2
p=1/2/3/4
t=0/1/2/3/4
p<5? y<br /> t>m? n. t now 1
p increments to 2
p<5? y<br /> t>m? n. t now 2
p increments to 3
p<5? y<br /> t>m? n. t now 3
p increments to 4
p<5? y<br /> t>m? y.
t increments to 4
m = p+t = 8
The correct answer is 4.
What did I do incorrectly?
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Caroline
You have correctly worked it out. The class is outputting t not m.
 
Caroline Bogart
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The devil's in the details... thanks!
 
Ranch Hand
Posts: 782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
U R RIGHT .Dont' Confuse.Try to compile & excecute code when u r confused or have any doubts.
Bye.
Viki.
------------------
Count the flowers of ur garden,NOT the leafs which falls away!
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, so I have a quick, somewhat off the subject question. In the comparison if(t++ > m) is done with m = 0 and t = 0 before the comparison is performed, then it will yield false, but t will be incremented to 1 after the comparison. Is that correct? However, what if the comparison was if(++t > m) with m = 0 and t = 0 before the comparison, then t will be incremented to 1 before they are compared, and the result will be true. Is this correct? Is this a result of operator precedence, or just the way t++ and ++t are defined?
Thanks,
Matt
[This message has been edited by Matt Wheeler (edited November 21, 2001).]
 
Vikrama Sanjeeva
Ranch Hand
Posts: 782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In this Case:

t will be first check by it's last value.For first time excution it will check by giving value 0.And after comparison has made it will be incremented to 1.

However, In this Case:
t will be first incremented & the it's invremented value will be used in comparison.That is for the first time execution t will be incremented to 1 & then compared with m.
Hope This Answers.
Bye.
Viki.

------------------
Count the flowers of ur garden,NOT the leafs which falls away!
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you're right Matt,
in ++t the comparison yields true.
In fact, unary operator (like ++) have a higher priority than binary ones.
This topic has been discussed many times here, but here it comes:
In t++, the value of t is stored and incremented right away. BUT the value used in the expression is the stored value not the result of the incrementation.
In ++t, the value is first incremented and then used so the result is available right away
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
Vikrama Sanjeeva
Ranch Hand
Posts: 782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here the ranchers has good deal with increment/decremnet operator.
Try It & Enjoy It. http://www.javaranch.com/ubb/Forum24/HTML/011163.html
Bye.
Viki.
------------------
Count the flowers of ur garden,NOT the leafs which falls away!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic