Why is this statement "so close, but still"?

I am working on an If statement and I want to fulfill two conditions to ignore the loop. It 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 go-to-work dilemma fails if radButton1 is false and radButton2 is true. Doesn't that require both conditions to be false in order to skip the statement?

0


source to share


7 replies


No, they require both of them to be false to execute.



+17


source


No, it requires both conditions to be false in order to execute the instruction. 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 txtTitle.Text to Go to Work."



If you want to skip a statement when both conditions are false, then negate your logic, e.g .:

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

      

This, translated into 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, skip it.

+12


source


Let's say I have two variables named A

andB

If A and B have these meanings

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 is the logical opposites (i.e. they did NOT apply) from the top 3 in the same table.

+9


source


In your example, it will ONLY execute code txtTitle.Text ="go to work"

if the BOTH buttons are false. So one of which is true and one is false, it will skip the statement.

+2


source


compared to true (or false) is completely unnecessary:

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

      

becomes

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

      

or equal

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

      

+2


source


Not. Omitting requires one of these statements. Look at your if:

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

      

So if condition1 OR condition2 is not true, it will not be met.

0


source


I got it working. I see that they must be false in order to execute the statement, for this statement the way to satisfy both conditions is to use

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

      

0


source







All Articles