Skip to content

Latest commit

 

History

History
164 lines (126 loc) · 8.22 KB

File metadata and controls

164 lines (126 loc) · 8.22 KB
title Chapter 3 - Statements, Expressions, Conditions & Variable Scope
weight 2

Chapter 3 - Statements, Expressions, Conditions & Variable Scope

What will you learn in this chapter

In this chapter you will learn about:

Statements, expressons, conditions and the variable scope are the foundation of every program or script.


Basic Statements

If your code is a recipe, the statements are the individual steps.

Statements are a way to tell the script engine what it needs to do. We've already used statements in previous chapters, such as when we performed assignment and declaration.

Expression Statements

Any expression can be placed on a line as a statement. Examples of expressions include:

  • Function call () operator - calls a function (more on functions later),
  • Equals = operator - performs assignment,
  • Add + operator - adds two values together,
  • Subtraction - operator - subtracts two values from each other,
  • Many more, such as +=, -=, ++, etc. are available. More about them can be found in the expression reference table.

Such statements need to end with a semicolon (;):

b = a + b;
func();
a += b;

AngelScript follows a specific instruction of operation order. First, function calls are evaluated bottom to top. AngelScript then sticks to the order of operations as specified in the Operator Precedence reference.

You can force an order of operations by using parentheses:

float a = 1 + 1.0 / 2; // This will return 1,5
float b = (1 + 1.0) / 2; // This will return 1

Tip

The order of how operators are evaluated for standard math operators such as +, -, *, /, etc. follow the standard Order of Operations.

TASK 1:

Given float a = 4; float b = 5;, write a program that calculates the value of c given by the equation c = (a - 2)^2 + (b + 1) / 2.

Comparison Operators

Comparison operators are operators that compare two values, such as with the true or false boolean values. An example of a comparsion operator is the equals (==) operator, which checks if the values on both sides of the operator are equal. Another example is the greater than operator (>), which checks if the value on the left side is greater than the value on the right side. For a more comprehensive comparison operator reference, please check the expression reference table.

int a = 1;
int b = 2;
a == b; // Will return false, but it won't save the value anywhere, treat this as a whole: (a == b)

b = 1;
bool result = a == b; // Result now stores the information if a and b were equal when it was defined.

Logic Operators

Logic operators are a way to combine comparison expressions in order to achieve one result:

  • NOT - denoted as !. Evaluates to true if value is false and vice versa.
  • AND - denoted as &&. Evaluates to true if both values are true.
  • OR - denoted as ||. Evaluates to true, if even one only of the values is true. Otherwise evaluates to false if both values are false.
  • XOR - denoted as ^^. Evaluates to True if and only if one of the values is True.
a && b; // AND
a && b || c; // A combination of AND and OR. a AND b will be evaluated first.
a && (b || c); // You can use parentheses to specify which operator should get evaluated first. Here, OR will get evaluated first.

Note

Although AngelScript supports Python-like logic operator notation (and, or, ...) it is recommended to stick to C-like notation. This is mainly because AS is much more similar to C in its syntax. In addition, not every extension adding AS support for your IDE has support for the Python-like notation.


Conditions

Conditions are a way to tell the engine to execute specific code only if a specific condition is met. For example, conditions will allow you to tell the engine to only add numbers together if they are positive. Conditions should contain an expression that evaluates to true or false. They can be any valid combination of comparison operators and logic operators, etc.

If/else block

The if/else if/else block is used to run specific code only on certain conditions. The else if and else are not required, but the block must start with an if statement.

if ( a > 10 ) { // Condition 1
    // Run the code in here
} else if ( a < 10 ) { // Condition 2
    // If we haven't met condition 1, but have met condition 2, execute this code
} else if ( condition3 ) {
    // We haven't met neither condition 1, nor condition 2, but we have met condition 3, execute this code
} else {
    // If none of the conditions were met, execute this code
}

TASK 2:

Given two numerical values a and b (defined statically in your script), print out an absolute value of their difference.

Switch Statement

The switch statement is a very useful tool for situations where you need to compare to a lot of different cases. It performs the is equal operation comparing a value to every value specified in case blocks. It is also much faster than a block of the if/else if/else statements.

switch ( value ) {
    case 0:
        // Do if value == 0
        break; // Required keyword to go out of the switch block
    
    case 2:
    case 3:
        // Execute if value == 2 or value == 3
        break;

    default:
        // Execute if neither cases were met
}

Caution

Each case requires a break statement after finishing code execution. This is because switch statements behave more like the goto (C++) functionality, meaning that it doesn't stop executing code after finishing a specific case. The break statement will tell the code to go out of the switch block. This is also why in the upper example case 2: and case 3: can be used to both execute the same lines of code.

TASK 3:

Given a string as a value (statically defined), write a program that would simulate a command-like behaviour using the switch statement. If the string is equal to:

  • "hello" - print "HelloWorld" to the console
  • "engine" - print "Strata Source" to the console
  • "name" - print "My name is Chell" to the console
  • Any other string - print "Command not recognized!" to the console

Variable Scope

Variable Scope determines which data can be accessed from where. In AngelScript, Variable Scope behaves exactly as the one in C++. In general, the Variable Scope makes programs use less memory by not holding expired information inside the memory. As an example:

int number = 10;

void my_func1() {
    int my_number = 21;
}

void my_func2() {
    number = 20; // Success, number has been reassigned to 20
    my_number = 80; // Error, my_number has not been declared
}

In this case, my_number doesn't exist inside my_func2, it only exists inside my_func1 and only for the duration of that function's execution.

Statements such as if, else, for, while, try, etc. create local variable scopes each time, meaning that variables declared inside them cannot be accessed outside of them, contrary to how, for example, Python handles it (where scopes are only created inside functions).

In AngelScript, global variables are allowed. However:

Note

From the official documentation: Variables of primitive types are initialized before variables of non-primitive types. This allows class constructors to access other global variables already with their correct initial value. The exception is if the other global variable also is of a non-primitive type, in which case there is no guarantee which variable is initialized first, which may lead to null-pointer exceptions being thrown during initialization.

Tip

A good practice is to avoid global variables altogether, and use different means of sharing information between functions such as returning values or &out references (more on that in later chapters).