Closed Thread Icon

Preserved Topic: boolean and while, me confused (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=21062" title="Pages that link to Preserved Topic: boolean and while, me confused (Page 1 of 1)" rel="nofollow" >Preserved Topic: boolean and while, me confused <span class="small">(Page 1 of 1)</span>\

 
Sash
Paranoid (IV) Inmate

From: Canada, Toronto
Insane since: May 2000

posted posted 02-14-2002 06:35

Again Java but also general.

In my book:

----------------------->-8-----------------------

code:
boolean done = false;
while (!done)
{ String line = console.readLine();
if (line == null)
done = true;
else
{ process data
}
}


This loop is a little different from the ones you saw before, because the test condition is a variable "done". That variable stays false until you reach the end of the input data; then it is set to true. The next time the loop starts at the top, "done" is true, and the loop exits.

----------------------->-8-----------------------

Question:

If we set boolean done = false and then in the loop say !done to me !done means not false, which means true. I know I am not right but I don't get it :-)
Any help, what here really happens?

Thanks.

sasha »

[This message has been edited by Sash (edited 02-14-2002).]

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 02-14-2002 06:44

Yup you are looking at it a bit skewed.

Lemme try to explain.
done is something, in particular it's a boolean so it's either true or false.

When you test something (done) or (!done) it has no idea what it's set at so it's just checking to see if it's true or false.

Just because you know that done has been set to false has no bearing to the syntax.

Jeez the semantics here are rough I hope I'm not just making things worse.





:[ Computers let you make more mistakes faster than any other invention in human history, with the possible exceptions of handguns and tequila. ]:

mr.maX
Maniac (V) Mad Scientist

From: Belgrade, Serbia
Insane since: Sep 2000

posted posted 02-14-2002 07:22

Sash, maybe it will make more sense if you look at that code (imagine this in your head, you don't need to write it like that if you don't want to) like this:

done = false;
while (done == false) {
}

That's the same thing as (!done), but you won't think of it as negation and how two false should be true...


Sash
Paranoid (IV) Inmate

From: Canada, Toronto
Insane since: May 2000

posted posted 02-14-2002 07:44

No worries bitdamaged, you didn't confuse me, just made me think more :-)

Ok, Max your code makes sense to me. Set done to false, loop, if the line is null, set done to true, exit the loop.

But how is while (done == false) the same as while (!done) if the variable is set to false previously?
In this case !done (where done is false) does not mean true?
Or is while(!done) shortcut for while(done == !done)?

[This message has been edited by Sash (edited 02-14-2002).]

mr.maX
Maniac (V) Mad Scientist

From: Belgrade, Serbia
Insane since: Sep 2000

posted posted 02-14-2002 07:59

(!done) can actually be shortcut for (done == false) and (done != true) and that's how you should look at it (like in my example from above), you don't need to bother yourself thinking about what done is already set to...


Sash
Paranoid (IV) Inmate

From: Canada, Toronto
Insane since: May 2000

posted posted 02-14-2002 08:05

Ok, I think I got it. Now I can go to sleep.

Thanks!

sasha &raquo;

« BackwardsOnwards »

Show Forum Drop Down Menu