aspose file tools*
The moose likes Beginning Java and the fly likes breaking out of while loop without condition being met? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "breaking out of while loop without condition being met?" Watch "breaking out of while loop without condition being met?" New topic
Author

breaking out of while loop without condition being met?

Tim Phifer
Greenhorn

Joined: Jan 23, 2009
Posts: 4
I'm having some trouble with what should be a fairly simple method. I pass an int N, and an array of size N + 1. The array should be populated with a list of non-repeating random integers. For example:



You can see where I've included some System.out.println's to help me see what is going on and here is the text from that:

For a specified N, this program generates
a random permutation of all numbers from 1 to N
using three different methods
Please enter the value of N: 5
Please select a method:
1: generate a number and test for repetitions
2: use additional array
3: using random swaps
1
1: j: 0 found: false cnum: 4
2: found: true
3: i: 0 found: true
1: j: 0 found: false cnum: 5
1: j: 1 found: false cnum: 5
2: found: true
3: i: 1 found: true
1: j: 0 found: true cnum: 4
2: found: true
3: i: 2 found: true
1: j: 0 found: false cnum: 5
1: j: 1 found: true cnum: 5
2: found: true
3: i: 3 found: true
1: j: 0 found: true cnum: 4
2: found: true
3: i: 4 found: true
4 5 0 0 0
number of operations: 0
run again? (y/n): n


So you see how the value of the boolean found is true, yet the method still seems to break out of the loop even though while(!found) should not be evaluating to true? I am just learning Java so it is probably something I'm missing, however reading through the textbook and searching around the interwebz hasn't yielded an answer nor a clue...
Dois Koh
Greenhorn

Joined: Jan 23, 2009
Posts: 20
umm

this
System.out.println("2: found: " + found);


is to show the value of "found" right?

and the output at that you've written says "2: found: true " right?

so since your loop is

while ( !found ), if found true, it won't loop right?

I mean.. its working as it should be isn't it?

Sorry, I'm a beginner as well so I may not be of much help.
Hope that helps though... If I didn't misunderstand you...
Tim Phifer
Greenhorn

Joined: Jan 23, 2009
Posts: 4
Hi Dois:

Well, my while ( !found ) statement has a negation operator " ! " so what I'm hoping for is when the value of found is true, the negation operator turns that statement into a false statement and repeats the loop.

So the fact that my debugging shows the value of found as true inside the loop, yet the next statement executed seems to occur outside of the loop, has me baffled.

Does that make sense?
Dois Koh
Greenhorn

Joined: Jan 23, 2009
Posts: 20
Ooooo

No offence but I think you got it wrong.

the while loop will continue to loop if the condition is met (true)

so while (true) will loop

and while (false) won't.

example: while ( a == 1 ) (the == is an operator that returns a boolean value... i think. In this case, if a is one, it will return "true" and it will continue looping)

in your case, found is true, so !found would return false.

so you end up with while(false). So it exits the loop
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874

Tim ,

The while loop is behaving as normal as it would be. To explain this

what would while (true) do ? , a infinite loop right ? so when found = true , !found evaluates to false , which makes while(false) . which means "get me out of teh loop" ?

i guess you have confused with the fact that while(false) will execute the loop. While loop runs as along the condition is met (which means true) , !found = false makes it out of loop. try while(found) to anlayse more ! Happy looping
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874


beaten me Dois you are spot on
Tim Phifer
Greenhorn

Joined: Jan 23, 2009
Posts: 4
Yeah, you're right. After I posted the reply I went back to the code with what you said and realized my error. No offense taken...

Kind of a dumb mistake on my part. However, I'm still not getting it to work the way I want. I'm going to try to rewrite the method from scratch and see if that helps. I've "tweaked" this so far already trying to get it to work that I think I'm missing something simple.
Dois Koh
Greenhorn

Joined: Jan 23, 2009
Posts: 20
ok good luck!
Tim Phifer
Greenhorn

Joined: Jan 23, 2009
Posts: 4
Okay, after starting over it seemed a bit easier to get it right. The following code works as desired:



Thank you both for pointing out the error in my logic. Who knows how long it might have taken to get it right without it having been pointed out.

Cheers!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38765
    
  23
Tim Phifer wrote:The following code works as desired: . . .
Well done
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: breaking out of while loop without condition being met?