Question

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?

Was it helpful?

Solution

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

OTHER TIPS

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.

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.

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 ) { ... }

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.

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.

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)))
{
    ...
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top