- Home /
Multiple Nested If Statements?
Hello,
What is the correct way to do multiple nested if's. The first if staement must be true and then I want Update to run through the rest of the nested if's only if the first one is true. Here is the example I have set up. I am not sure if I am using the open and close brackets correctly for my purpose and I am not sure if I should use else if's in this scenario. I will be useing this code in Update. Thanks in advance.
if (1 == a) {
if (2 ==b)
{
// Do a set of actions
}
if (2 ==c)
{
// Do a set of actions
}
if (2 ==d)
{
// Do a set of actions
}
if (2 ==e)
{
// Do a set of actions
}
}
How about this modified arrangement? Wouldn't this work as well.
if (1 == a) {
if (2 ==b)
{
// Red
}
else if (2 ==c)
{
// Green
}
else if (2 ==d)
{
// Yellow
}
else
{
// Blue
}
}
For argument sake here is a scenario with the above code:
If 1==a is true and 2==d is true, Yellow would happen and the rest of the 'if' conditions would be ignored.
If 1==a is true but none of the other 'if' conditions are true Blue would happen.
But if 1==a is false then Update will not bother checking the rest of the 'if' conditions.
Are the above senarios correct?
The solution you gave will match with the behaviour you are trying to achieve ;)
The new example is not entirely correct
If 1==a is true and 2==d is true, Yellow would happen and the rest of the 'if' conditions would be ignored.
this is only the case if 2==b and 2==c are not true.
In the example you are describing you seem to have 4 independent int variables whose values are not connected to each other in any way and the way you use them indicates they have some kind of order of priority. (a is the most important one deter$$anonymous$$ing if anything will be done, b is the second in order and if it equals 2, the other conditions don't matter) If this sounds wrong, you would probably be better off rethinking the type and setup of of the variables you are comparing in your IFs. (what i'm getting at is whether b, c and d can all be 2 at the same time, or is that a situation that wouldn't make sense)
I wonder if you could give a real world example of what kind of situation we are dealing with here ? Right now you only check for values of 1 and 2 but if you have more values to check in the future and need to check for different combinations, you'll end up with an else-if that grows exponentially every time a new value is added.
Otherwise, to clarify your doubts about how ifs work: the ELSE of an IF will never happen if the IF happened, and
if (something)
{
// do a
}
else if (somethingElse)
{
// do b
}
is logically exactly the same as
if (something)
{
// do a
}
else
{
if (somethingElse)
{
// do b
}
}
Those are pretty much the only two rules you need to know about the execution order/rules
Also, it's small potatoes but the syntax in your "if"s is what is known as "Yoda conditions"
i.e. it's easier to read the conditions when they are written as you would say them out loud "If you have red hair, you're a redhead" ins$$anonymous$$d of "If red is the colour of hair you have, you're a redhead"
Thank you for your explanation. The first 'if' is the most important. The rest of the 'if' statements are equel. I thought it would be good to set it up this way so Update is not checking all of the 'if' statements unnecessarily.
Besides the first 'if' statement only one of the nested 'if' statements will be true one at a time.
Answer by kemar · Jul 31, 2015 at 01:22 PM
You can use a switch statement if you have a lots of conditions
int testValue = 2;
if (1 == a) {
switch(testValue)
{
case b:
// Do a set of actions
break;
case b:
// Do a set of actions
break;
case c:
// Do a set of actions
break;
case d:
// Do a set of actions
break;
default:
// Do a set of actions
break;
}
}
The if if if inside the initial if 1= are all exclusive (2 is either b,c,d,e per OP) so switch/case seems fine (structured inside the wrapping if 1) as you have it here kemar.
Actually you can't do a switch case like that since the cases require a constant value and in this case they'd be variables a, b, c ...
Your answer
![](https://koobas.hobune.stream/wayback/20220611233835im_/https://answers.unity.com/themes/thub/images/avi.jpg)