Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by TheCallipo · Feb 04, 2017 at 07:46 PM · if-statementsiforderconditionoperator

Execution order of conditional operators in an if-statement

Heyho,

can anybody explain me in which order the operators or in which 'blocks' would check an IF-Statement?

For Example: I want to check if one player has two more points than the other AND the current round is greater then 3.

 if (player1WinCount == player2WinCount + 2 || player2WinCount == player1WinCount + 2 && roundNumber >= 3)

I'm not sure if this would work. Does this code check if one player has two more wins and THEN checks if the roundNumber is greater-equal 3 or do I have to put the '&& roundNumber >= 3' on both sides of the OR (||) operator? Like this:

 if (player1WinCount == player2WinCount + 2 && roundNumber >= 3 || player2WinCount == player1WinCount + 2 && roundNumber >= 3)

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by HenryStrattonFW · Feb 04, 2017 at 10:55 PM

Conditions get processed first to last, but keep in mind that you can often avoid duplicate in logic statements, for example in your case, you can do the OR operation first on the players win counts, and then check the round number, like so.

 if ((player1WinCount == player2WinCount + 2 || player2WinCount == player1WinCount + 2) && roundNumber >= 3)


Additionally, if you ever get a bit confused with larger combinations of conditional checks, you can just break them up to make things clearer to read, the cost of a few extra booleans really wont hit you that hard at all. so you could do something like this as well.

 bool isThirdRoundOrMore = (roundNumber>= 3);
 bool isOnePlayerTwoAhead = (player1WinCount == player2WinCount + 2 || player2WinCount == player1WinCount + 2);
 
 if (isThirdRoundOrMore && isOnePlayerTwoAhead)
Comment
Add comment · Show 1 · 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 TheCallipo · Feb 07, 2017 at 03:18 PM 0
Share

that helped me a lot thanks :)

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

90 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

The If statement condition is false but the if statement stills executes 1 Answer

Help with rotation please 2 Answers

how do i say this 1 Answer

Unity Update is ignoring the GetKey part of my statement!? 2 Answers

Randomly choosing between 3 options 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