[c#] What is the OR operator in an IF statement

In C#, how do I specify OR:

if(this OR that) {do the other thing}

I couldn't find it in the help.

Update:

My code is:

if (title == "User greeting" || "User name") {do stuff}

and my error is:

Error 1 Operator '||' cannot be applied to operands of type 'bool' and 'string' C:\Documents and Settings\Sky View Barns\My Documents\Visual Studio 2005\Projects\FOL Ministry\FOL Ministry\Downloader.cs 63 21 FOL Ministry

This question is related to c# conditional-operator

The answer is


Or is ||

And is &&

Update for changed question:

You need to specify what you are comparing against in each logical section of the if statement.

if (title == "User greeting" || title == "User name") 
{
    // do stuff
}

you need

if (title == "User greeting" || title == "User name") {do stuff};

The reason this is wrong:

if (title == "User greeting" || "User name") {do stuff};

is because what that's saying is

If title equals the string "User greeting"

or just "User name" (not if title equals the string "User name"). The part after your or would be like writing

if ("User name")

which c# doesn't know what to do with. It can't figure out how to get a boolean out of "User name"


In the format for if

if (this OR that) 

this and that are expression not values. title == "aaaaa" is a valid expression. Also OR is not a valid construct in C#, you have to use ||.


See C# Operators for C# operators including OR which is ||


Just for completeness, the || and && are the conditional version of the | and & operators.

A reference to the ECMA C# Language specification is here.

From the specification:

3 The operation x || y corresponds to the operation x | y, except that y is evaluated only if x is false.

In the | version both sides are evaluated.

The conditional version short circuits evaluation and so allows for code like:

if (x == null || x.Value == 5)
    // Do something 

Or (no pun intended) using your example:

if (title == "User greeting" || title == "User name") 
    // {do stuff} 

The OR operator is a double pipe:

||

So it looks like:

if (this || that) 
{
  //do the other thing
}

EDIT: The reason that your updated attempt isn't working is because the logical operators must separate valid C# expressions. Expressions have operands and operators and operators have an order of precedence.

In your case, the == operator is evaluated first. This means your expression is being evaluated as (title == "User greeting") || "User name". The || gets evaluated next. Since || requires each operand to be a boolean expression, it fails, because your operands are strings.

Using two separate boolean expressions will ensure that your || operator will work properly.

title == "User greeting" || title == "User name"

The conditional or operator is ||:

if (expr1 || expr2) {do stuff}

if (title == "User greeting" || title == "User name") {do stuff}

The conditional (the OR) and it's parts are boolean expressions.

MSDN lists the C# operators in precedence order here http://msdn.microsoft.com/en-us/library/6a71f45d.aspx . And the MSDN page for boolean expressions is http://msdn.microsoft.com/en-us/library/dya2szfk.aspx .

If you are just starting to learn programming, you should read up on Conditional Statements from an introductory text or tutorial. This one seems to cover most of the basics: http://www.functionx.com/csharp/Lesson10.htm .


OR is used as "||"

 if(expr1 || expr2)
 {
    do something
 }