Click here to Skip to main content
15,867,771 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
hi.
i want to know how can i use or like using it in:
if some thing or something else happened or not do .... .
if (.... ll .....)
{

}
how can i use it in C#??
[Edit]Removed Email[/Edit]
Posted
Updated 3-Aug-11 15:05pm
v2
Comments
Dr.Walt Fair, PE 3-Aug-11 17:58pm    
Do you understand C# syntax? Using a logical OR operator is really basic.
Sergey Alexandrovich Kryukov 3-Aug-11 18:00pm    
This is my concern, too. I answered in detail (please see), but does it worth it?
--SA
Sergey Alexandrovich Kryukov 3-Aug-11 18:00pm    
I answered, but... asking such elementary and general questions is a very ineffective way to learn programming. Put more effort in reading of C# and .NET manual and doing simple exercises -- you will learn everything much faster and deeper.
--SA

1 solution

There are two Boolean "OR" operators: "|" and "||". First one checks up both left and right Boolean operands in all cases, the second one "||" optimizes the check: if first operand is true, there is no need to check up the second operand.

Why not using "||" all the time? Well, this is what I do. But "|" can be needed in case the second Boolean operand is a function with the side effect. If the call is optimized out using "||", the side effect will disappear. The operator "|" will not allow it.

Example:

C#
bool Right() {
   bool result = //... calculate the result
   System.Console.WriteLine("Right returned {0}", result); //this lines does side effect
   return result;
}

//...

bool left = true;

//...

if (left || Right())
   //...
   // the result of Right will never be printed for left == true;

//...

if (left | Right())
   //...
   // the result of Right will always be printed


A very typical Boolean expression for the operand is arithmetic comparison expression:
C#
double a, start, end;

//...

if (a < start || a > end)
   System.Console.WriteLine("The value {0} is outside the segment [{1} .. {2}]", a, start, end);


Same thing with Boolean "&&" and "&" operators. The operator "&&" will skip checking of the right operand if the left operand is false.

Don't mix up Boolean "|" and binary (set) operator "|"; don't mix up Boolean "&" and binary (set) operator "&".

—SA
 
Share this answer
 
v4
Comments
BillWoodruff 3-Aug-11 20:34pm    
+5 Can't see why anyone #1 voted this post, which is accurate, thoughtful, and took time to write. The OP's question is so vague it's hard to know what he's asking, but why punish somebody who cares enough to write an answer ?
Sergey Alexandrovich Kryukov 3-Aug-11 21:30pm    
Thank you very much, Bill.
As to the vote of 1, it happens all the time and not a big problem. I face low votes mostly when a question if very simple or silly. Understandable, come to think about.
--SA
Mohammad A Rahman 4-Aug-11 0:17am    
I agree. Sometimes people voted 1 and don't even bother to write a comment. It is frustrating. I hope we should change our mentality about voting........:(

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900