So, what was fishy in this code -- except for my typo in the second for-loop -- was 
the assignment

i = i--

The correct version of the first for-loop would simply have had "i--" or "--i", 
instead of "i=i--".

If you have an assignment j = i--, it's clear what's going to happen: j gets the value 
of i, and
after that i is decremented. In this case it's not so clear what is supposed to 
happen.  In C (or
C++), if i==n, then after executing i=i--, i turns out to have the value n-1. The 
funny thing is
that in Java i will still have the value n.

I am tempted to think that C does it right and Java does it wrong, so I'm tempted to 
see this as a
bug in the Java language (I also wonder if all compilers produce the same code).  The 
Java language
specification does not seem to specify unambiguously how i=i-- should be interpreted.
(http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#39438)

Hans


----- Original Message -----
From: "Geuns, Hans" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Thursday, May 09, 2002 10:35 AM
Subject: [cscie119] Bug of the month


> What is the complexity of the following code (how fast does it run)?
>
> for (int i=n; i > 0; i = i--)
> for (int j=1; j < 1; j = j*2)
> print(j);
>
>
> (Students who attended section yesterday are not elligible for prizes.
> Thanks to Tom Faiano for pointing out that there was something funny with
> this code fragment from the practice final.)
>
> Hans
>
>


Reply via email to