# Nested if statements and composite conditions

Nested if statements and composite conditions
0

#1

Here’s an example to understand nested conditions versus composite conditions

Say we code up this statement

Bring an umbrella if it’s raining but wear a raincoat if it’s windy

Here’s one way to interpret the statement and express it in code

``````let rainy // boolean true if raining false otherwise
let windy // boolean true if windy false otherwise
let umbrella // boolean true if umbrella needed false otherwise
let raincoat // boolean true if raincoat needed false otherwise

// assume rainy and windy have some values
``````

This expression uses composite conditions

``````// block 1: composite condition
if(rainy && windy) {
umbrella=false
raincoat=true
} else if(rainy && !windy) {
umbrella=true
raincoat=false
} else {
umbrella=false
raincoat=false
}
``````

This formulation uses nested conditions

``````// block 2: nested condition
if(rainy) {
if(windy) {
umbrella=false
raincoat=true
} else {
umbrella=true
raincoat=false
}
} else {
umbrella=false
raincoat=false
}
``````

Blocks 1 and 2 are equivalent in that for any given values of `rainy` and `windy` the combined values of `umbrella` and `raincoat` are the same after either block executes

Now what can we say about the following variants?

``````// block 3: composite without first else of block 1
if(rainy && windy) {
umbrella=false
raincoat=true
} else {
umbrella=false
raincoat=false
}

// block 4: nested condition without inner else of block 2
if(rainy) {
if(windy) {
umbrella=false
raincoat=true
}
} else {
umbrella=false
raincoat=false
}
``````

Blocks 3 and 4 are not equivalent to blocks 1 and 2 in that for some values of `rainy` and `windy` the combined values of `umbrella` and `raincoat` do not match the combined values of the pair after block 1 or block 2

This is apparent since two boolean variables `umbrella` and `raincoat` have four possible combinations of values - blocks 1 & 2 establish 3 distinct combinations of values for the variables but blocks 3 and 4 only establish 2 combinations

None of these blocks is right or wrong - each represents some understanding of the English statement - they are however different understandings resulting in different outcomes

Profile Lookup Question [SOLVED]
#2

I think the other thing to think about when faced with nested conditionals (and switch statements too) is not only how do I represent them better while nesting, but also how can I eliminate the nesting or reduce the need for it.

For example,
Given:

``````let useRaincoat = false;
let useUmbrella = false;
``````

When:

``````let isWindy = false;
let isRainy = false;
``````

Then:

``````useRaincoat = (isRainy && isWindy); /* don't use a raincoat */
useUmbrella = (isRainy && !isWindy); /* don't use an umbrella */
``````

OR When:

``````let isWindy = false;
let isRainy = true;
``````

Then:

``````useRaincoat = (isRainy && isWindy); /* don't use a raincoat */
useUmbrella = (isRainy && !isWindy); /* do use an umbrella */
``````

OR When:

``````let isWindy = true;
let isRainy = true;
``````

Then:

``````useRaincoat = (isRainy && isWindy); /* do use a raincoat */
useUmbrella = (isRainy && !isWindy); /* don't use an umbrella */
``````

And of course you could also make these test functions as well:

``````function isUmbrellaWeather(isRainy, isWindy){
return  (isRainy && !isWindy);
}
function isRaincoatWeather(isRainy, isWindy){
return (isRainy && isWindy);
}

useRaincoat = isRaincoatWeather(true, false); /* don't use a raincoat */
useUmbrella = isUmbrellaWeather(true, false); /* do use an umbrella */
``````

And you could probably refactor those two functions down even further using arrow functions:

``````const isUmbrellaWeather = (isRainy, isWindy) => isRainy && !isWindy;
const isRaincoatWeather = (isRainy, isWindy) => isRainy && isWindy;
``````