Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by r_chevallier · Jul 31, 2015 at 08:45 AM · if-statementsifnestedif else

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
   }

}

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image r_chevallier · Jul 31, 2015 at 07:59 AM 0
Share

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?

avatar image Hellium · Jul 31, 2015 at 08:09 AM 0
Share

The solution you gave will match with the behaviour you are trying to achieve ;)

avatar image NoseKills · Jul 31, 2015 at 12:57 PM 0
Share

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"

avatar image r_chevallier · Jul 31, 2015 at 08:23 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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;
     }
     }
      


Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image getyour411 · Aug 05, 2015 at 10:24 PM 0
Share

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.

avatar image NoseKills · Aug 06, 2015 at 03:21 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Object doesn't move 2 Answers

Input.GetKey(KeyCode.E) requires multiple presses. 1 Answer

Decreasing a value within the if statement ! 0 Answers

Translate doesnt work inside IF statement, but does outside. 2 Answers

Use multiple values for "if" statement 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges