Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 instruct9r · Sep 15, 2013 at 05:04 PM · ifelsestatementand

If \ Else how does the program reads it?

ookkay.. I have one questions about if else.. I had a problem with a piece of code and when i added "else if", intead of just "else" it started working okay. So i was wondering what happens if i have && in the if statement and just "else"...

Let's say that i have this condition

 if (objectA.position.x == 0.0 && objectB.position.y == 10.0)
 {
      ... do some actions
 }
 else
 {
      ... stop doing the actions
 }

When i have && in hte "if" statement and only "else", does Unity reads it as:

 else if (objectA.position.x != 0.0 && objectB.position.y != 10.0)

or

 else if (objectA.position.x != 0.0 || objectB.position.y != 10.0)

on the else statement, if only one is false and the other true, will the else be executed, or it will continue with the first statement to be true? Or if some of the 2 statements are not true, then both if and else will be false and none will be executed?

I know that it's better in this case to add some statement in the else if, but i was curious how it works..

thanks :) , intead of just

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

2 Replies

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

Answer by Hoeloe · Sep 15, 2013 at 05:28 PM

The else keyword is used as a performance optimisation, which allows some code to execute given a previous if statement didn't. Basically, the else block will only be executed if the corresponding if statement wasn't. So, if you have:

 if(a && b)
 //run code
 else
 //run more code

it is logically equivalent to:

 if(a && b)
 //run code
 if(!a || !b)
 //run more code

(assuming the values of a and b are not modified in the first if statement)

because !a || !b is the condition under which the if statement is not executed. The benefit of the else statement over this second option, though, is that the execution is faster, because the code just needs to check if it ran the previous condition, rather than re-evaluating the expression. Also, else if is logically identical to a standalone else block if an if statement inside - the if is just part of the block, so when you write:

 else if(a) {...}

you can think of it as:

 else
 {
     if(a){...}
 }

Meaning that the conditional will not be evaluated if the corresponding if statement was found to be true - again, this is a very useful performance optimisation.

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 instruct9r · Sep 15, 2013 at 08:56 PM 0
Share

Got it, thanks. It was little bit confusing for me, but when i see my code again, now i know why it was't working as expected with just "else"..

Thanks for the optimisation hints..

avatar image
2

Answer by YoungDeveloper · Sep 15, 2013 at 05:31 PM

In the first condition, you are basically checking if(something is true AND something is true), if one of them will not be true, whole if statement won't be launched. Or operator ||, will be launched if least one statement is true.

 if(1 == 1 && 1==2) {//wont be launched, because both must be true ( 1 isn't 2)}
 
 if(1 == 1 || 1==2) {//will be launched, because at least one of these guys are true}

Else, only launches if the previous "if" statement wasn't successful, so if the "if" is true, next else will be ignored.

Now for the else if, if lets say we have 5 "else if" statements,

 if(2 == 1){//wont be launched}
 else if(3 == 2){//wont be launched}
 else if(5 ==5 ) {//will be launched AND all next "else if" will be ignored till next "if"}
 else if(1 == 1) {//will be ignored and not run, because one of the previous "else if" was true }
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 instruct9r · Sep 15, 2013 at 08:56 PM 0
Share

Got it, thanks for the info

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

17 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

Related Questions

How to finish my else if statement? 2 Answers

How to make an if statement with two conditions? 1 Answer

if else statement doesnt work 1 Answer

|| inside if statement not working. (noob) 3 Answers

Check renderer.enabled statement from another object 3 Answers


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