Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

&& or ||?

Posted on 2006-04-13
15
Medium Priority
?
155 Views
Last Modified: 2010-04-16
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
0
Comment
Question by:paulwhelan
15 Comments
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 80 total points
ID: 16446360
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
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16446367
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
 
LVL 11

Expert Comment

by:vo1d
ID: 16446422
hi paulwhelan,

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

greets, vo1d
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16446442
Haha...I didn't even notice that the last test is checking a "subfield" of owner (the second test)...

Good catch vo1d!

=)
0
 
LVL 11

Expert Comment

by:vo1d
ID: 16446541
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
 
LVL 11

Expert Comment

by:vo1d
ID: 16446557
no problem man ;)
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16446621
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
 
LVL 11

Expert Comment

by:vo1d
ID: 16446654
yes it works, but its not the way it should be used ;)
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16446684
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
 
LVL 11

Expert Comment

by:vo1d
ID: 16446858
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
 
LVL 13

Expert Comment

by:devsolns
ID: 16447521
vb6 ewwwww, i always hated the lack of circuit breaking the most with vb expressions.  and the binary use of = operator.
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 16448469
&& 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
 
LVL 11

Expert Comment

by:Expert1701
ID: 16448484
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
 
LVL 11

Expert Comment

by:vo1d
ID: 16448840
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
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16448865
@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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question