1. Member
Join Date
May 2011
Posts
39
Rep Power
0

## Logical Operator query

Hello,

why does the below work correctly only with the || operator as I thought that I should use the && operator

Java Code:
```import java.util.Random;

public class Dice {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

int dice1;
int dice2;
int count;
int totalcount = 0;

do{
Random rand = new Random();

dice1 = rand.nextInt(6) + 1;

dice2 = rand.nextInt(6) + 1;

System.out.println("Dice1 " + dice1);
System.out.println("Dice2 " + dice2);

count = dice1 + dice2;

totalcount = totalcount + 1;

System.out.println("Count for this throw is " + count);

[B]}while(dice1 != 1 || dice2 != 1);[/B]

System.out.println("");
System.out.println("");

System.out.println("You have obtained the Snake Eyes by doing a total count of " + totalcount);

}

}```

2. why does the below work correctly only with the || operator as I thought that I should use the && operator
Java Code:
`(dice1 != 1 || dice2 != 1)`
Define what "work correctly" means?
Please explain what the conditions are that you want the condition to be true?
Make a truth table listing all the values for the two die with the desired results.

3. Member
Join Date
May 2011
Posts
39
Rep Power
0
Hi Sorry I just want the do while loop to stop if both dices numbers are 1 only

4. It seems the code works. So what is your problem?

5. Originally Posted by aconti
Hi Sorry I just want the do while loop to stop if both dices numbers are 1 only
So you do want the loop to continue as long as either of the dice is not 1.

db

6. Moderator
Join Date
Apr 2009
Posts
11,302
Rep Power
18
Or, put another way, you want to exit the loop when both dice equal 1:
dice1 == 1 && dice 2 == 1
which means to continue the loop you want that to be false:
!(dice1 == 1 && dice 2 == 1)
which is, of course, the logical equivalent to the OR one you have in your functioning code.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•