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 /
This question was closed Feb 08, 2015 at 09:57 AM by meat5000 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Digital-Phantom · Feb 08, 2015 at 07:19 AM · c#if statementelseunexpected

Why is one line of code correct but the other gives Errors? (Solved)

Why is this line of code correct-

 if(Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))
             return true; else return false;
 

and yet this line of code gives me a compile error -

 if(Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))
 {
 return true; else return false;
 }
 

From what I understand, curly brackets are sometimes optional. But I don't see why adding them in this case suddenly makes the 'else' statement unexpected ?

What is it in this case the brackets change about the code ???

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

  • Sort: 
avatar image
1
Best Answer

Answer by tanoshimi · Feb 08, 2015 at 08:13 AM

Curly braces denote a block of code. When used following an if/else condition, they signify the lines of code to be run if a condition is true, and the lines to be run if not. Without curly braces, the compiler just considers the single line of code following the if/else statement.

In contrast, indenting and putting statements on new lines does not make a difference to how the compiler interprets your code, but it makes it easier for you to see mistakes. Look at your examples again, but this time use correct indenting.

This is your first example:

 if(Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))
     return true;
 else
    return false;

This is the second:

 if(Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))
 {
     return true;
     else
        return false;
 }

Do you see the problem? The else is not in the same scope as the if.

The learning point? Always use curly braces, but also indent those blocks of code, and always put statements on new lines.

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 Digital-Phantom · Feb 08, 2015 at 08:31 AM 0
Share

It wasn't actually 'my' code, but that from a tutorial I'm following. As I'm still learning I've been doing like you suggested and using braces whenever possible. Once I used the braces I started to get an error but was unsure as to why.

Thanks to you guys I now at least understand why.

avatar image tanoshimi · Feb 08, 2015 at 08:42 AM 1
Share

I don't know what tutorial you're following, but it's considered very bad coding practice not to place statements on new lines - I would be somewhat cautious of some of the correctness of other things they might be $$anonymous$$ching you....

avatar image
1

Answer by mattyman174 · Feb 08, 2015 at 08:00 AM

  if(Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))
 {
 return true; else return false;
 }

By enclosing the else within the brace of the if statement, you are reducing its scope to within the brace and thus causing it to be without an if statement.

If you had the else outside of the closing brace of the if statement it would be a valid else statement.

  if(Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))
 {
   return true; 
 }
 else return false;
Comment
Add comment · 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

Follow this Question

Answers Answers and Comments

21 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

I have a specific problem with my trigger 2 Answers

Larger Than Smaller than not working 1 Answer

Application OpenUrl 'Unexpected Symbol Application' 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