# WHILE LOOP USING || (or) SIMPLE PROBLEM

• May 6th, 2012, 11:43 PM
SPACE MONKEY
WHILE LOOP USING || (or) SIMPLE PROBLEM
Dear all:

I thought that a while statement of : (argument || argument), where at least one of the arguments was correct, the do loop attached to the while would end.

QUESTION IS AT VERY END OF POST.

Code snippet works if you want to try it.

Code :

```  import java.util.Scanner; import java.lang.Math; public class FixChap5 { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); int x = 1; boolean b = false; do { if ( x%2 == 0) b = true; else b = false;   x++; } while (!b || x < 7 ); System.out.println(" x = " + x); }}```

So the above bit of code works and gives you X = 7.

Without the: || x < 7 in the while line, the code stops at x = 3.

Value of A = True , Value of B = False, Combined value of A||B = True

QUESTION 1.
So why is the do loop still going around when it should have stopped because b = false and thus printed out x = 3. Instead it waits until both arguments are correct and then stops at x = 7.

QUESTION 2.
I wanted to end a while statement with one of two different arguments being true, how do it do it?
• May 7th, 2012, 12:01 AM
pbrockway2
Re: WHILE LOOP USING || (or) SIMPLE PROBLEM
Quote:

So why is the do loop still going around when it should have stopped because b = false and thus printed out x = 3. Instead it waits until both arguments are correct and then stops at x = 7.
Because that's what "or" means! The point is that the "or" gives two possible reasons for the while loop to keep going: the more alternatives you put in the while condition, the longer the loop may keep going.

Quote:

QUESTION 2.
I wanted to end a while statement with one of two different arguments being true, how do it do it?
Remember that the do loop will finish when the condition becomes false, so you are looking for a statement that becomes false when one of its two halves is true. Consider "!foo && !bar" and "(!foo && !bar) || (foo && bar)". They are different (deliberately).
• May 7th, 2012, 12:25 AM
SPACE MONKEY
Re: WHILE LOOP USING || (or) SIMPLE PROBLEM
Dear pbrockway2:

Quote:

Remember that the do loop will finish when the condition becomes false
I was confused, I was looking for the loop to finish when one argument was TRUE.

But the loop to finishes when the result is FALSE.

In my case, with one argument being false and the other argument being true, the final answer returned to the while method is TRUE.

So the loop continues on another turn, instead of ending.

Thanks for the help, another day of pain and blindness ended. :)

How do I show this thread has been solved?
• May 7th, 2012, 01:23 AM
pbrockway2
Re: WHILE LOOP USING || (or) SIMPLE PROBLEM
You're welcome.