Xpode.com        Click here to Print this article.

AndAlso & OrElse Operators in C#

First we review the & operator and | operator. What & operator do. If there are two conditions then it will check both conditions even first one is false. It will process the second condition also. Assume in second condition It is a function which takes 2 seconds for process, why we compare that condition if first one is false. Ok now I will show you a example.



int a = 20;
int b = 1;
if (b > 1 & a/b <100)
{
Response.Write("Both conditions run because we have write & operator");
}

Now we will take example of && (also called AndAlso in vb.net) operator. If we will use the && operator in above example then it will execute only the first condition. Because first condition is not true so it will not process the second condition. It will save the overhead of comparing the second condition.

int a = 20;
int b = 1;
if (b > 1 && a/b <100)
{
Response.Write("Only first condition will run because we have write && operator.");
}

Same in || (also called OrAlso in vb.net) condition. If first condition is true then it will not go further. It will execute the second condition if first condition is false.

int a = 20;
int b = 1;
if (b = 1 II a/b <100)
{
Response.Write("Only first condition will run because we have write || operator");
}


http://
http://

Contributed by:
Rohit kakria
I am software developer

Resourse address on xpode.com
http://www.xpode.com/Print.aspx?Articleid=146

Click here to go on website