Results 1 to 7 of 7
Thread: Why won't this while loop work?
- 05-23-2009, 12:39 AM #1
Member
- Join Date
- Mar 2009
- Posts
- 57
- Rep Power
- 0
Why won't this while loop work?
Good day,
I am trying to use this method but the literals "add", "subtract" or "divide" are not stopping the while loop from iterating. Why? Is there a better or correct way of doing this? Many thanks.
public String getOperation()
{
String operatorSelect = Dialog.request("Please input add, subtract or multiply");
while(!operatorSelect.equals("add") || !operatorSelect.equals("subtract") || !operatorSelect.equals("multiply"))
{
Dialog.alert("This is not a valid operator, please try again");
operatorSelect = Dialog.request("Please input add, subtract or multiply");
}
return operatorSelect;
}
-
You may want to use Venn diagrams to help you visualize this type of problem. You'll see that your while condition encompasses all possible conditions -- is always true. Now try drawing the Venn diagram using && instead of ||, and see what comes out.
- 05-23-2009, 04:39 AM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Simply read a bit about logical operators in Java. Then you can have a clear idea about the || and && operators, and what's the most suitable for yours.
- 05-23-2009, 11:48 AM #4
Member
- Join Date
- Mar 2009
- Posts
- 57
- Rep Power
- 0
Thanks for your inputs.
The loop works fine when || is replaced with && or ^ but the statement didn't seem to read right?
OR seemed the right thing to say rather than AND....
-
Your first statement is equivalent to this:
Now tell me when the boolean condition in the while statement will ever be false? Do you see the fallacy here?Java Code:int x = getUserInput(); while ((x != 2) || (x != 3) { x = getUserInput(); }
If the user enters 2, the second statement is true and because it uses an or operator, the whole statement is true. Same for 3, and same for any number entered. Your logic is the same. Again, learn to use Venn diagrams as most of us understand this better visually. Best of luck.
- 05-23-2009, 07:04 PM #6
Member
- Join Date
- Mar 2009
- Posts
- 57
- Rep Power
- 0
Ah yes, I see what you're saying - the compound expression will always evaluate to true because if the user inputted int variable equals 2 and returns false for the first part of the compound expression then it will evaluate to true for the second part because 2 does not equal 3.
Many thanks for your help and insightfulness.
-
Similar Threads
-
my loop doesn't work.. pls help???
By ashton in forum New To JavaReplies: 5Last Post: 01-16-2009, 08:24 AM -
I need help with my work assig Please
By tracy-london in forum New To JavaReplies: 3Last Post: 01-04-2009, 05:50 PM -
Just how do I get Java to actually work?
By MickY G in forum New To JavaReplies: 5Last Post: 11-19-2008, 03:50 AM -
Why doesn't my loop work?
By d0nmin0 in forum Advanced JavaReplies: 8Last Post: 05-26-2008, 06:56 PM -
how would i get this to work...?
By deeadeed in forum New To JavaReplies: 6Last Post: 12-06-2007, 02:58 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks