Click here to hide categories Click here to show left categories

User: Home          welcome : Guest          Log In / Register here     




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");
}
Share this article   |    Print    |    Article read by 8841 times
Author:
Rohit kakria
I am software developer
Related Articles:
Related Interview Questions: