Previous Lecture Complete and continue  

  Statements

When you code in C#, you need to tell the system to execute your instructions (e.g., print information) using statements. A statement is literally an order or something that you ask the system to do. For example, in the next line of code, the statement will tell Unity to print a message in the Console window:

print (“Hello Word”);

When writing statements, you will need to follow several rules, including the following:

  • The order of statements: each statement is executed in the same order as it appears in the script. For example, in the next example, the code will print hello, then world; this isbecause the associated statements are in that particular sequence.
  • Statements are separated by semi-colons (i.e., semi-colon at the end of each statement).
  • For example, the next line of code has a correct syntax, as all of its statements are separated by a semi-colon.
  • Multiple spaces are ignored for statements; however, it is good practice to add spaces around the operators +, -, /, or % for clarity. For example, in the next code snippet, we say that a is equal to b. You may notice that spaces have been included both before and after the operator =.
  • Statements to be executed together (e.g., based on the same condition) can be grouped using code blocks. In C#, code blocks are symbolized by curly brackets (e.g., { or }). So, in other words, if you needed to group several statements, you would include all of them within the same set of curly brackets, as follows:
  • Printing a message in the Console window: the keyword is print.
  • Declaring a variable: the keyword, in this case, depends on the type of the variable that is declared (e.g., int for integers, string for text, or bool for Boolean variables), and we will see more about these in the next sections.
  • Declaring a method: the keyword to be used depends on the type of the data returned by the method. For example, in C#, the name of a method is preceded by the keyword int when the method returns an integer; it is preceded by the keyword string when the method returns a string, or by the keyword void when the method does not return any information.
  • Marking a block of instructions to be executed based on a condition: the keywords are if and else.

print (“hello”);

print (“world”);

Note that several statements can be added on the same line, as long as they are separated by a semi-colon.

print(“hello”);print (“world”);

a = b;

{

print (“hello stranger!”);

print (“today, we will learn about scripting”);

}

As we have seen earlier, a statement usually employs or starts with a keyword (i.e., a word that the computer knows).Each of these keywords has a specific purpose, and common keywords, at this stage, could be used for the following actions:

Exiting a function: the keyword is return.

You can drag and drop the file called Statements to an empty object and play your scene; You can also modify it if you wish to try other statements.

Download