Reputation: 303
I have an if statement that I need to check more than one condition in.
For example:
if (a == 0 and b != 0 or c != 0)
{
//do something
}
What is the most efficient way to accomplish this?
Upvotes: 1
Views: 25668
Reputation: 799
Use the &&
(and) operator with the ||
(or) operator.
if (a == 0 && b != 0 || c != 0)
{
// Do something
}
Upvotes: 5
Reputation: 162
Use && operator and || operator.
&& - this is called and operator || - this is called or operator
if (a == 0 && b != 0 || c != 0)
{
//do something
}
Upvotes: 0
Reputation: 174
In C, the logical operator, for "AND" is, &&
.
Similarly, for "OR", it is, ||
.
To evaluate some parts as a single statement, use brackets:
In a && (b || c)
, a
will be evaluated first, if true then (b || c)
will be evaluated together.
Upvotes: 1