At 11:00 PM 1/6/2002, you wrote:
> > > > 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++) {
> > > >  7.        if(t++ > m) {
> > > >  8.          m = p + t;
> > > >  9.        }
> > > > 10.      }
> > > > 11.      System.out.println("t equals " + t);
> > > > 12.    }
> > > > 13.  }

:)  First of all, this is a *servlet* interest group, so this is severely
off-topic.  So please, if you reply again, reply to me personally, I'll be
happy to help you.  Second, this is pretty basic java stuff that hopefully
in the future you can figure out with a java book, an interactive debugger,
and/or by putting println()s *inside* your loop.

Why is p==5 when the loop finishes?  Because the "p < 5" is the while
condition.  P is incremented until it equals 5, at which point the loop
finishes.  The equalivalent code would be:
while (p < 5)
{
         ...
         p++;
}

So t is 4 because the loop is executed four times (p==1...p==4), and you
increment it once per loop.

Meanwhile, you have set m to always be greater than t, and you coded it as
t++ not ++t, so that is why it always does the m = p + t line.  Equivalent
code for the most inner block of code would be:
         if (t > m) {
                 t = t + 1;
                 m = p + t;
         }
         else t = t + 1;
That is, because you said t++, if first checks to see if t > m, then it
increments t, then if t was greater than m, it does the m = p + t.

Hope this is clear,

-- Jim
***************************************************************************************
* Jim Lindsay
* Occassional Java Instructor & currently looking for work programmer
* [EMAIL PROTECTED]
* 510-527-8025
* 510-681-6484 (cell)
* 510-528-8317 (fax)
* 555 Pierce St. #141
* Albany, CA 94706
*
* My home page: http://www.jerel.com/jim
* Albany Schools: http://www.albany.k12.ca.us/
* Mediation: http://www.bdrs.org
* Electoral Reform: http://www.fairvoteCA.org
*****************************************************************

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://archives.java.sun.com/jsp-interest.html
 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.jsp
 http://www.jguru.com/faq/index.jsp
 http://www.jspinsider.com

Reply via email to