[NSRCA-discussion] Chapter-12 computer science.

Martin X. Moleski, SJ moleski at canisius.edu
Wed Jul 4 05:57:43 AKDT 2007


Duane Beck wrote:

> num is incremented in the body of the loop, before cnt is incremented at 
> the end of the loop (right before it checks the loop condition again). 
> So, num equals 1 when cnt is incremented to 1 at the end of the loop, 
> before the condition is checked a second time.  Next time through the 
> loop, num is incremented to 2 before cnt increments to 2.  When cnt 
> equals 256 at the loop condition check (assuming it's able to represent 
> such a value), num will have already been incremented to 256 in the 
> previous pass through the loop body.

DOH!

Got it.

cnt passes the test 256 times, not 255 (from 0 to 255),
so num gets incremented to 256, not 255.

I can't count how many bugs I've generated by being
"off by one."  :o(

> In general, it's poor practice to use chars for integer math.  If you 
> need a small integer, use short instead.

Yes, I totally missed that the return type was char.

Thanks for the refresher in computer logic.  It may
not make me a better pattern pilot, but loops are
involved in both disciplines.  :-O

				Marty



More information about the NSRCA-discussion mailing list