user1767754
user1767754

Reputation: 25094

Multiple Conditions in If Statement or Splitting

when i am using if statements with multiple conditions, how are they managed in the compiler?

A) Will it ignore the second Statement, if the first statement is not fulfilled or vice versa?

If(time > 3.0 && hitEnabled)

B) Late Defintions are often recommended, so should i prefere to use one condition in if statements?

if(time > 3.0)
    if(hitEnabled)

Thanks!

Upvotes: 0

Views: 6959

Answers (7)

Sriram Sakthivel
Sriram Sakthivel

Reputation: 73452

if(time > 3.0 && hitEnabled)

In above statement hitEnabled will not be evaluated when time > 3.0 is false.

This is called short-circuit.

Following statement will evaluate hitEnabled even when time > 3.0 is false but returns true when both operands are true.

if(time > 3.0 & hitEnabled)//note bitwise &

if(time > 3.0)
    if(hitEnabled)

Nested if statements are helpful when you need first condition to be checked many times etc.

if(time > 3.0 && hitEnabled)
{
//DoSomething1
}
if(time > 3.0 && flag)
{
//DoSomething2
}

This can be re written with nested if statements as follows

if(time > 3.0)
{
    if(hitEnabled)
    {
    //DoSomething1
    }
    if(flag)
    {
    //DoSomething2
    }
}

In this case I prefer nested if statement to avoid unnecessary checks

Upvotes: 3

Vamsi
Vamsi

Reputation: 51

If the first condition is violated it wont check for the second one and it simply ignores. If you want to put any code it is always better to make it nested.

Upvotes: 0

digital_revenant
digital_revenant

Reputation: 3324

In case of an && if the first condition is false, the second condition will never be evaluated and the overall result is false. In case of an ||, if the first condition is true the second condition is not evaluated and the overall result is true. As Rob pointed out, it is known as short circuit evaluation.

This is useful in cases when we want to evaluate the second operand of the if statement only if the first operand returns true. For example, we may want to check for the validity of a variable before using it.

if(ptr != NULL && *ptr > x)

In this case the value of ptr will be checked against x only if it is not NULL.

Upvotes: 2

user2771704
user2771704

Reputation: 6202

A) It won't check hitEnabled if first condition will be false, if you want to do it you must use short-circuit AND (&) like below. Even if first condition will be False, it will check second one.

If(time > 3.0 & hitEnabled)

B) It strongly depends on what you want from you application and less on performance of your hardware. If you want to check both conditions in any case, B option is perfectly fine, but if you certain if you time > 3.0 is false and you don't want check second one, A option is preferable in my opinion. As I said before it is strongly depends on the logic of your program, so you can't get the right answer based on one line of code.

If you ask just about what better manner of writing without logic background, it's up to you. Both variants easy to read, if you follow code conventions.

Upvotes: 1

Tafari
Tafari

Reputation: 3059

In the first case:

If(time > 3.0 && hitEnabled)

If time > 3.0 is false then hitEnabled will never be checked. And it always starts checking conditions from left to right.

If you want to make sure that all conditions will be checked you should use || (logical OR) instead of && (logical AND) for example:

If(time > 3.0 || hitEnabled)

This:

if(time > 3.0 && hitEnabled)

equals

if(time > 3.0)
if(hitEnabled)

Upvotes: 0

Goran
Goran

Reputation: 93

        String someString = null;

        if (someString != null && someString[4].Equals('a'))
        {
            //// not called
        }

        if (someString != null || someString[4].Equals('a'))
        {
            //// exception
        }

        Console.ReadLine();

Upvotes: 0

David Arno
David Arno

Reputation: 43254

The evaluation of the expression stops as soon as it is conclusively true or false.

This allows expressions like if (x != null && x.property ... as x.property will not be evaluated if x is null etc.

Upvotes: 0

Related Questions