Syntax and Semantics – 26/01/21

Syntax and Semantics are parts of programming that mean separate things in relation to one another.

Syntax

Syntax is the structure or the grammar of a programming language. It describes the way to construct a correct sentence. Errors that occur are a violation of programming rulesw within the set language.

Semantics

Semantics relates to the meaning of a programming language. If everything is syntactically correct and there are no errors, it doesn’t necessarily mean you will get the output desired.

Programming and Operators

OperatorUsage
+Addition
Subtraction
*Multiplication
/Division
=Assigns // Equals
==Is Equal To
!=Is Not Equal To

Debug.Log

Use Debug.Log to print informational messages that help you debug your application. For example, you could print a message containing some text used to describe a line in your code or if a condition has been met.

As an example debug.log can be used to verify some things, such as this complicated maths script I made within unity. If the answer is correct, it outputs ‘good maths’ if its incorrect, it says ‘eh?’.

Programming Symbols

There are a variety of programming symbols that we use but the most common ones are

;A semi-colon lets the compiler know you’ve reached the end of a statement. It’s a bit like a full stop, for code.
{ }These are parentheses. They’re used to group sections of code together. When operating with parentheses, every bracket that is open needs to have a closing bracket.
( )These are standard brackets. Commonly used to hold parameters. Similarly used in maths.

Breakpoints

Breakpoints are pieces of code that stop the code from running when it comes to a certain point, this allows the developer to inspect the code and see what has gone wrong and where it has gone wrong.

When a breakpoint is reached, the script goes in to break mode, this allows the developer to do numerous things.

  • inspect and make adjustments to variables.
  • Stop the script from running.
  • Step through code line to line.
  • Move the execution point so as to resume the application, execution from that point.

Without a debugger, this can become far more complicated as you wouldn’t have anything to refer to if there were any errors.

Bug Fixing and Common Errors

Common mistakes that can occur with programming

  • Capitalisation where it matters, if a variable is named score, it simply cannot be Score.
  • Every { must have a } to close it. Same thing applies to any bracket such as [] and ().
  • Statements and lines have a ; at the end of it, this does not apply for if statements unless it is inside of the if.

Code Fixing Challenge

The bool needed to be set to true and the grammar in the if statement was incorrect.
From top to bottom there should be no curly bracket at the end of monobehaviour and no semi colon after the first if statement, the first if statement is also missing a bracket at the end.

Leave a comment