- Home /
The question is answered, right answer was accepted
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 ???
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.
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.
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....
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;
Follow this Question
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