jshbrntt
jshbrntt

Reputation: 5404

Simplifing if statement?

Is it possible to simplify this if statement?

and if so what's the answer?

    if (type)
    {
        if(NdotL >= 0.0)
        {
            color   += Idiff + Ispec;
        }
    }
    else
    {
        color   += Idiff + Ispec;
    }

Upvotes: 2

Views: 115

Answers (4)

Sam Walls
Sam Walls

Reputation: 185

Use

if (type && NdotL > 0.0){
   Blah....
} else {
   Buegh...
}

Just so that it combines the two conditions.

Really sorry about indentations and such, but the mobile version of this site doesn't let you enter code, I just wanted to help you so much XD!

Upvotes: 1

Floris
Floris

Reputation: 46415

Think about this in terms of Boolean algebra. You have two conditions

A = (type)
B = (NdotL >= 0.0 )

And you execute your statement when

A * B
/A

( I use /A to indicate "NOT A", and * to indicate "AND" )

So the only time you don't execute is

A * /B

This means your statement should be

if (!((type) && NdotL < 0.0 )) {
  // do your thing
}

Or, using Boolean identity

(A * B) = /(/A + /B)

you can rewrite your condition as

( /A + B )

if ( !(type) || ( NdotL >= 0 ) ) {
   // do your thing
}

Upvotes: 6

Lee
Lee

Reputation: 144176

if (!type || NdotL >= 0.0)
{
    color += Idiff + Ispec;
}

Upvotes: 5

L. Quastana
L. Quastana

Reputation: 1336

Try that :

color+=(type && NdotL >= 0.0)? Idiff + Ispec:Idiff + Ispec;

Upvotes: 0

Related Questions