&& or ||?

Sorry for the confusing title

I want to do something like this

if ( (element == null) || (element.owner == null) || (element.owner.isdead = true) )
...

But im unsure of when to use && or ||

if the element is null i obviously dont want it to check the next two parts

Thanks
Paul
paulwhelanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike TomlinsonMiddle School Assistant TeacherCommented:
The C# logical operators && and || shortcircuit by default:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csspec/html/vclrfcsharpspec_7_11.asp

So change it like so...

    if ( (element != null) && ( (element.owner == null) || (element.owner.isdead = true) ) )

Then it won't evalute the second half if the first test fails.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mike TomlinsonMiddle School Assistant TeacherCommented:
I copied your statement there...we should have an equality check at the end, not an assignment:

    if ( (element != null) && ( (element.owner == null) || (element.owner.isdead == true) ) )
0
vo1dCommented:
hi paulwhelan,

if((element != null) && (element.owner != null) && element.owner.isdead)
{
   ...
}

greets, vo1d
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Mike TomlinsonMiddle School Assistant TeacherCommented:
Haha...I didn't even notice that the last test is checking a "subfield" of owner (the second test)...

Good catch vo1d!

=)
0
vo1dCommented:
booleans are NOT checked with the == operator.

true is:
if(element.owner.isdead)
{...}

false is :
if(!element.owner.isdead)
{...}

one more question, what do you wanna do, if element is initialized?
do you wanna directly check in the next step the two other statements?
0
vo1dCommented:
no problem man ;)
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
The == operator works for booleans (at least it just did for me in C# 2005 Express):

            bool flag = false;
            if (flag == true)
            {
                MessageBox.Show("True");
            }
            else if (flag == false)
            {
                MessageBox.Show("False");
            }

?...
0
vo1dCommented:
yes it works, but its not the way it should be used ;)
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
I think that's more a matter of personal preference...I come from a VB.Net background and think the more verbose version is more readable...but we digress!  ;)
0
vo1dCommented:
i come from c++, so i prefer the other way :)
another advantage of my way is that the compiler generates less code.
check it out using ildasm.
your statement generates two more opcodes.



0
devsolnsCommented:
vb6 ewwwww, i always hated the lack of circuit breaking the most with vb expressions.  and the binary use of = operator.
0
Arthur_WoodCommented:
&& is the C# logical operator for LOGICAL AND
|| is for LOGICAL OR

in VB.net, you would use AND (&&) and OR(||)

In VB.NET

If x = 5 AND y = 3 then
   ' do something
Else
   ' do something else
End iF

in C# this would read

if (x == 5 && y == 3)
{
   // do something
}
else
{
  // do something else
}

AW
0
Expert1701Commented:
vo1d, I checked it out, and the two methods,

  bool flag = false;
  if (flag == true)
  {
    Console.WriteLine("True");
  }
  else if (flag == false)
  {
    Console.WriteLine("False");
  }

and

  bool flag = false;
  if (flag)
  {
    Console.WriteLine("True");
  }
  else if (!flag)
  {
    Console.WriteLine("False");
  }

generate identical MSIL:

  .maxstack  1
  .locals init ([0] bool flag)
  IL_0000:  ldc.i4.0
  IL_0001:  stloc.0
  IL_0002:  ldloc.0
  IL_0003:  brfalse.s  IL_0011
  IL_0005:  ldstr      "True"
  IL_000a:  call       void [mscorlib]System.Console::WriteLine(string)
  IL_000f:  br.s       IL_001e
  IL_0011:  ldloc.0
  IL_0012:  brtrue.s   IL_001e
  IL_0014:  ldstr      "False"
  IL_0019:  call       void [mscorlib]System.Console::WriteLine(string)
  IL_001e:  ret


The only way you are going to avoid a couple opcodes is to skip the second comparison, i.e.,

  bool flag = false;
  if (flag)
  {
    Console.WriteLine("True");
  }
  else
  {
    Console.WriteLine("False");
  }

which results in the following MSIL:

  .maxstack  1
  .locals init ([0] bool flag)
  IL_0000:  ldc.i4.0
  IL_0001:  stloc.0
  IL_0002:  ldloc.0
  IL_0003:  brfalse.s  IL_0011
  IL_0005:  ldstr      "True"
  IL_000a:  call       void [mscorlib]System.Console::WriteLine(string)
  IL_000f:  br.s       IL_001b
  IL_0011:  ldstr      "False"
  IL_0016:  call       void [mscorlib]System.Console::WriteLine(string)
  IL_001b:  ret

The moral of the story is the if(variable) vs. if(variable==true) makes no difference to the compiled application.
0
vo1dCommented:
Expert1701, you are right with the opcode.
dont know, why i got different codes.
i tried it again and it was the same as your code.

but lets have the following code:

int x = 1;
int y = 0;
if (x > y)
{
   Console.WriteLine("True");
}
else
{
  Console.WriteLine("False");
}

you would not write it like this or would you?

int x = 1;
int y = 0;
if ((x > y) == true)
{
   Console.WriteLine("True");
}
else
{
  Console.WriteLine("False");
}

its a matter of how you deal with the evaluation of the if statement.



0
Mike TomlinsonMiddle School Assistant TeacherCommented:
@Arthur_Wood...

You're two statements are NOT equivalent.

To short circuit in VB.Net you need to use "AndAlso" instead of "And":
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcn7/html/vaconlogicaloperators.asp

    If x = 5 AndAlso y = 3 Then
       ' do something
    Else
       ' do something else
    End If
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.