Click here to Skip to main content
15,885,546 members
Articles / Programming Languages / C#

Difference between && and &, || and |

Rate me:
Please Sign up or sign in to vote.
4.26/5 (33 votes)
3 Oct 2013CPOL2 min read 106.4K   18   24
In this article we will learn a very important and basic part of the AND and OR operators.

Introduction

Hello everyone, in this article we will learn a very important and basic part of the AND and OR operators. It is not dependent on the language; it’s just logic and optimization. As the title of the article suggests, it’s all about two and and single and or two or and single or operators. So let’s start.

Difference between && and & 

&& is called the AND operator and & is also called the AND operator but the basic difference between them is in the way they are executed. The syntax for && and & the same as in the following:

  • bool_exp1 && bool_exp2
  • bool_exp1 & bool_exp2

Now the syntax of 1 and 2 looks  similar to each other but the way they will execute is entirely different.

In the first statement, first bool_exp1 will be executed and then the result of this expression decides the execution of the other statement. If it is false then the AND will be false so it makes no sense to execute the other statement. The bool_exp2 statement is executed if and only if bool_exp1 returns true on execution. It is also known as the short circuit operator because it shorts the circuit (statement) on the basis of the first expression’s result. Let’s understand this using an example.

C#
int x = 0;
if (5 < 4 && (7 / x) == 0)
 MessageBox.Show("Won't execute!");
else
{
 MessageBox.Show("&& wont execute the 7/0 so no divide by zero error.");
}

The output of the code above is:

&&a

Now in the case of & things are different. The compiler will execute both statements and then the result will be ANDed. It’s an inefficient way of doing things because it makes no sense to execute the other statement if one is false because the result of AND is effective only for ANDing results evaluated to “true” and it’s possible when both statements are true.

Example:

C#
int x = 0;
if (5 < 4 &  (7 / x) == 0)
MessageBox.Show("Won't execute!");
else
{
MessageBox.Show("&& wont execute the 7/0 so no divide by zero error.");
}

Output:

single and output.

Difference between “||” and “|”

It’s the same as above, in the case of “||” only one statement is executed and if it returns “true” then the other statement will not be executed. But if the first is false then the other will be checked for the value “true”. The reason for this is the way the “or” operator works. The “Or” operator depends on only one true, in other words if any of the expressions are true then the result will be true.

Example:

C#
int x = 0;
if (5 > 4 || (7 / x) == 0)
  MessageBox.Show("|| executed because 5>4 evaluates to true.No divide by zero error.");
else
{
 MessageBox.Show("Won't execute!");
}

Output:

or2

That’s OK but the way “|” behaves is the same as that of “&”, in other words both statements are executed regardless of the result of one statement. Check the following example and it will be clear.

C#
int x = 0;
if (5 > 4 | (7 / x) == 0)
MessageBox.Show("|| executed because 5>4 evaluates to true.No divide by zero error.");
else
{
  MessageBox.Show("Won't execute!");
}

Output:

or1

Summary

Thanks for reading this article.  This article is just for information purposes, I won’t recommend anyone to use “&” or “|” instead of “&&” or  “||”. Don’t forget to comment and share this article. 

Edit 1 

As pointed out by Klaus Luedenscheidt in comment, This article doesn't deal with  the original use of '&' and '|' . These operators are very useful in performing bitwise operations. And also you can not use '&&' and '||' for performing bitwise operations. I guess this is the only reason for their existence.   

A sort example : 

printf("%d",4&5); //will print  4 because.

00001000  //4 in binary  
00001001 //5 in binary
________ // Perform & (anding of bits) 
00001000 //4 in binary  

printf("%d",4|5); //will print  5 because 

00001000  //4 in binary   
00001001 //5 in binary  
________ // Perform & ('Or'ing of bits) 
00001001 //5 in binary       

License

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


Written By
Student
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionNice explanation Pin
Member 1455802527-Sep-19 11:33
Member 1455802527-Sep-19 11:33 
QuestionNice one Pin
Shashank Laxman19-Dec-17 0:27
Shashank Laxman19-Dec-17 0:27 
QuestionMy vote of 5 Pin
Member 112055697-Dec-17 2:40
Member 112055697-Dec-17 2:40 
GeneralMy vote of 3 Pin
pradiprenushe19-Nov-13 17:03
professionalpradiprenushe19-Nov-13 17:03 
GeneralMy vote of 3 Pin
Debopam Pal15-Nov-13 6:57
professionalDebopam Pal15-Nov-13 6:57 
Question[My vote of 1] not great Pin
BillW3315-Nov-13 3:14
professionalBillW3315-Nov-13 3:14 
AnswerRe: [My vote of 1] not great Pin
Debopam Pal15-Nov-13 6:54
professionalDebopam Pal15-Nov-13 6:54 
GeneralMy vote of 5 Pin
Rolando CC14-Nov-13 8:50
professionalRolando CC14-Nov-13 8:50 
GeneralMy vote of 5 Pin
JS0000114-Nov-13 5:26
JS0000114-Nov-13 5:26 
GeneralMy vote of 3 Pin
Rakshith Kumar11-Nov-13 22:41
Rakshith Kumar11-Nov-13 22:41 
QuestionNice Attempt Pin
Rakshith Kumar11-Nov-13 22:41
Rakshith Kumar11-Nov-13 22:41 
GeneralMy vote of 2 Pin
ROman_R10-Nov-13 21:37
ROman_R10-Nov-13 21:37 
QuestionGood Initiative Pin
Debopam Pal10-Nov-13 17:57
professionalDebopam Pal10-Nov-13 17:57 
Questionhai Pin
Member 103139074-Oct-13 10:45
Member 103139074-Oct-13 10:45 
QuestionCool Pin
Dmitry A. Efimenko4-Oct-13 8:37
Dmitry A. Efimenko4-Oct-13 8:37 
GeneralMy vote of 2 Pin
Leslie K.4-Oct-13 7:21
Leslie K.4-Oct-13 7:21 
GeneralRe: My vote of 2 Pin
Arpit Jain4-Oct-13 7:42
Arpit Jain4-Oct-13 7:42 
I didn't dismissal the use of bit wise operators. This is not a tutorial on how to use bit wise operators (and not even about conditionals). This article is stating only that use '&' and '|' only for bit wise operations. For conditional expressions use the short-circuit version because they are much better then "& and |".

In simple words "Use conditional (&& and ||) in conditions and bit wise operators(& and |) for performing bit wise operations. "DO NOT MIX" Smile | :)
Arpit Jain

Questiongreat Pin
kesav aggarwal4-Oct-13 6:09
kesav aggarwal4-Oct-13 6:09 
GeneralMy vote of 2 Pin
gicalle754-Oct-13 2:43
professionalgicalle754-Oct-13 2:43 
GeneralRe: My vote of 2 Pin
Arpit Jain4-Oct-13 4:35
Arpit Jain4-Oct-13 4:35 
GeneralMy vote of 5 Pin
Santosh K. Tripathi4-Oct-13 1:44
professionalSantosh K. Tripathi4-Oct-13 1:44 
QuestionThanks Pin
Member 97383063-Oct-13 1:23
Member 97383063-Oct-13 1:23 
SuggestionUsing as bitwise operator Pin
Klaus Luedenscheidt2-Oct-13 18:11
Klaus Luedenscheidt2-Oct-13 18:11 
GeneralRe: Using as bitwise operator Pin
Arpit Jain3-Oct-13 4:21
Arpit Jain3-Oct-13 4:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.