aspose file tools*
The moose likes Beginning Java and the fly likes Endless loop ! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Endless loop !" Watch "Endless loop !" New topic
Author

Endless loop !

Rahul Sudip Bose
Ranch Hand

Joined: Jan 21, 2011
Posts: 637

why does this program not exit when i type n or N ? The program exits when i keep c!='n' in the while loop and enter n while running the program.



SCJP 6. Learning more now.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4422
    
    8

Rahul Sudip Bose wrote:

So you wan't to keep going as long as c isn't equal to 'n' OR c isn't equal to 'N'. Which will always be true, because c can't be both! That should give you a hint.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11411
    
  16

System.out.println("") is a VERY powerful debugging tool. When I have something like this happen, I liberally stick them in my code. If you think the code should be exiting, print out the value of what you're testing right before you test it...in other words, I would add a line between your current 15 and 16, like



(I print the semi-colon so I know exactly where the variable ends - i.e. I can tell if there are trailing spaces.)

try that, and see what you can figure out.





There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
Good idea of Fred's. But I shall expand it
Rahul Sudip Bose
Ranch Hand

Joined: Jan 21, 2011
Posts: 637

Campbell Ritchie wrote:Good idea of Fred's. But I shall expand it


The above way is a little difficult for me as of now because I am a beginner. I tried it this way instead :



THE OUTPUT IS :



how do i correct this ?
Rahul Sudip Bose
Ranch Hand

Joined: Jan 21, 2011
Posts: 637

Matthew Brown wrote:
Rahul Sudip Bose wrote:

So you wan't to keep going as long as c isn't equal to 'n' OR c isn't equal to 'N'. Which will always be true, because c can't be both! That should give you a hint.


ahhh ... DOH !!! it works with this simple change :


But i will try it using the != logic also, as an exercise.

Thanks to all.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
or



. . . while (c != 'n' && c != 'N');
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
De Morgan's Laws

¬a ∧ ¬b ≡ ¬(a ∨ b)
¬a ∨ ¬b ≡ ¬(a ∧ b)

You can use those to swap && and ||
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Endless loop !