tags:

views:

460

answers:

7

I am working on an If statement and I want to satisfy two conditions to ignore the loop. This seemed easy at first, but now... I don't know. this is my dilemma...

if((radButton1.checked == false)&&(radButton2.checked == false))
{
    txtTitle.Text = "go to work";
}

The dilemma is "go to work" is not executed if radButton1 is false and radButton2 is true. Shouldn't it require both conditions to be false in order to skip the statement?

+17  A: 

No, it requires them to both be false to execute the statement.

gbarry
+12  A: 

Nope, it requires both conditions to be false to execute the statement. Read again:

if ((radButton1.checked == false) && (radButton2.checked == false)) {
    txtTitle.Text = "Go to work";
}

In English: "If radButton1.checked is false AND radButton2.checked is false, then set the txtTitle.Text to 'Go to work'".

If you want to skip the statement when both conditions are false then negate your logic, like this:

if ((radButton1.checked == true) || (radButton2.checked == true)) {
    txtTitle.Text = "Go to work";
}

This, translated to English would read: "If radButton1.checked is true OR radButton2.checked is true, then set the text to 'Go to work'". This means that if any condition is true, it will execute the statement, or, if both are false, to skip it.

Vinko Vrsalovic
I'm sorry about that. I want it to excecute the command if they are both false, but it seems to jump if one condition is false.
You need to place a debugger and see what's going on... you might think one is true, but may not be the case
Vinko Vrsalovic
+2  A: 

In your example it will ONLY execute the txtTitle.Text ="go to work" code if BOTH buttons are false. So one being true and one being false it will skip the statement.

Mitchel Sellers
A: 

No. It requires one of those statements to be false to skip. Look at your if:

if (condition1 && condition2) {
    doSomething();
}

So if condition1 OR condition2 is not true then it won't execute.

Elie
+8  A: 

Say I have two variables named A and B

If A and B have these values

A     true    true    false   false
B     true    false   true    false

then these operations return

AND   true    false   false   false
OR    true    true    true    false
XOR   false   true    true    false
NAND  false   true    true    true
NOR   false   false   false   true
XNOR  true    false   false   true

Note that the bottom 3 in the second table are the logical opposites (i.e. they've had NOT applied) of the top 3 in the same table.

R. Bemrose
A: 

I got it to work. I see that they both need to be false to execute the statement, for that statement, the way to satisfy both conditions is by using

if(!((radButton1.checked == true)&&(radButton2.checked == true)))
{
    ...
}
Andrew
Lulu, gbarry and Vinko have tried their best to explain what is what? After that also you could not decide what you want. Your modified code and first code snippet is doing exactly the same thing. I think you need to go and get your basics clear.
Pradeep
+2  A: 

comparing to true (or false) is entirely unnecessary:

if(!((radButton1.checked == true)&&(radButton2.checked == true))) { ... }

becomes

if( !(radButton1.checked && radButton2.checked) ) { ... }

or equally

if( !radButton1.checked || !radButton2.checked ) { ... }
Draemon
thats what I was thinking.And it looks a lot cleaner
Miles