sampy
sampy

Reputation: 303

How to check two conditions in if statement?

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

Answers (3)

Aleksandar Matic
Aleksandar Matic

Reputation: 799

Use the && (and) operator with the || (or) operator.

if (a == 0 && b != 0 || c != 0)
{
    // Do something
}

Upvotes: 5

Theri Muthu Selvam
Theri Muthu Selvam

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

iceeggocean
iceeggocean

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

Related Questions