# why???

• 02-25-2011, 07:08 AM
glina126
why???
why doesnt this code run!?

Code:

``` while ((randomNumber1 != userNumber1 && randomNumber2 != userNumber2 && randomNumber3 != userNumber3 && randomNumber4 != userNumber4));```

i wrote a simple mastermind game and once the numbers match i want the do loop to stop. it stops even if only one of the numbers match :confused:
• 02-25-2011, 07:29 AM
pbrockway2
Quote:

it stops even if only one of the numbers match

Suppose we have

Code:

```randomNumber1 is 1    userNumber1 is 100 randomNumber2 is 2    userNumber1 is 200 randomNumber3 is 3    userNumber1 is 3 randomNumber4 is 4    userNumber1 is 400```

(ie only one match)

Think about the bits that make up the condition:

Code:

```randomNumber1 != userNumber1  is  true randomNumber2 != userNumber2  is  true randomNumber3 != userNumber3  is  false randomNumber4 != userNumber4  is  true```

So the combined condition is true&&true&&false&&true which is, of course, false. Hence the loop stops.
• 02-25-2011, 07:37 AM
glina126
well then why does it still stop if i have this code?

Code:

``` while ((randomNumber1 == userNumber1 && randomNumber2 == userNumber2 && randomNumber3 == userNumber3 && randomNumber4 == userNumber4));```

i just did a visualization for myself.

false
false
true
false

if following your example. how can i write this then???
• 02-25-2011, 07:40 AM
pbrockway2
Same thing.

Consider the values as before where there is only one match and break your condition up into its four components. What four truth values do you get for them?
• 02-25-2011, 07:49 AM
glina126
I did this and it works :D thanks so much

Code:

``` x++;                                                                 if (randomNumber1 == userNumber1 && randomNumber2 == userNumber2 && randomNumber3 == userNumber3 && randomNumber4 == userNumber4)                                 {                                         x = 8;                                 }                                 }                                 while (x <= 7);```
no more late night programming for me. period. thanks a bunch!