Reader Level:
ARTICLE

Difference Between && And &, || And |

Posted by Arpit Jain Articles | Coding Best Practices September 13, 2013
In this article we will learn a very important and basic part of the "and" and "or" operators.
  • 1
  • 0
  • 3972

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:

1. bool_exp1 && bool_exp2

2. 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 the 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.

1
2
3
4
5
6
7
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 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:

1
2
3
4
5
6
7
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:

1
2
3
4
5
6
7
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.

1
2
3
4
5
6
7
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.

COMMENT USING

Trending up