Difference between | and ||

 Posted by Ddd on 1/20/2011 | Category: C# Interview questions | Views: 2615 | Points: 40
Answer:

| : logical OR

||: Conditional OR

Both operators are used to check the OR condition.

The differences:

1)if the first condition evaluates to TRUE, then

| : it will check the second condition also.

||: it does not check the second condition

2) If the first condition evaluates to FALSE, | and || both will check the second condition

Take these 2 examples:

class dd
{
static void Main()
{

int a = 10, b = 0;

if (a==10 | (a%b)==0)
{
Console.WriteLine("yes");
}
//output will be an error message
}

class dd
{
static void Main()
{

int a = 10, b = 0;

if (a==10 || (a%b)==0)
{
Console.WriteLine("yes");
}
//output will be yes
}


Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Login to post response