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 ???

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;

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.