-
Notifications
You must be signed in to change notification settings - Fork 8
2 Variables & Operators
As in other programming languages, SGL uses variables in order to store data. A Variable is like a little memory which you can access through an identifier. Every variable is also of a specific variable type. This type tells the compiler how to deal with your given data, because you can do different actions and methods with different types of variables. As you will see, it also helps you to prevent errors in your code.
There are 2 types of variable types: primitive and object types. With primitive types, you can store information like letters or numbers so their use is pretty intuitive. Object types however can store sprites (pictures) and animations you will need in order to generate your storyboard.
Those are the primitive types which are used in SGL:
-
boolean - Can store 2 values: true and false and are often used in conditions and loops
-
int - Can store all integer values (from –2^31 to 2^31–1) like 573 or -3
-
float - Can store all floating-point values (from 4,9E-324 to 1,8E+308) like 65.132 or -2.6
-
string - Can store an (almost) unlimited number of characters like "Hello World"
Currently, there is only one object type:
- object - For storing Sprite and Animation objects
In order to use variables as a storage for your data, a variable needs to be declared first. The syntax for this statement is allways:
variableType variableName;Note that you have to put a semicolon at the end, because you have to do so at the end of all statements. After the declaration, variables will contain a default value (like 0 for int or false for boolean).
Here I defined some (local) variables for you to have an example:
int bottles;
string content;
boolean empty;
float liters;Along with the declaration of a variable, you can assign a value to it. You do that by using the assignment operator =. For this example, we will initialize variables with simple lyterals:
int bottles = 4;
string content = "beer";
boolean empty; // empty is 'false' per default, but you could also write 'boolean empty = false;' of course
float liters = 1.5;You can declare more than one variable at once, just separate them with a comma:
int days, minutes = 30, hours = 4;
// This is the same as writing
int days;
int minutes = 30;
int hours = 4;Note that the assignment operator = means "assign the right-hand side to the variable on the left-hand side". That means it's not an equal sign in the mathematic sense, so don't get confused: In SGL, you can say i = i + 1; which means "first calculate i + 1 and then assign it to i", but mathematically this statement would be invalid, because i and i + 1 can never be the same.
After you've declared a variable, you can assign values to it. Yo do so by using the assignment operator mentioned above. You can declare things like literals or complex mathematical expresions:
int a;
a = 4;
println(a); // 4
a = 2 * (a + 4) // a was 4, so it's the same as 'a = 2 * (4 + 4)'
println(a); // 16We've already used a few operators in the examples. This chapter gives you an overview of all the operators you can use in SGL.
These operators are used with numbers (int and float) and will also return a new number.
-
+Adds the left to the right value / Negates the following expression -
-Subtracts the left from the right value -
*Multiplies the left by the right value -
/Divides the left by the right value -
%Modulo operation (divides the left by the right value and returns the remainder)
Also pay attention to the order of operation rules, e.g. a multiplication will be calculated before an addition, but you can use brackets to change this - just like in maths ;)
println(2+4); // 6
println(2+4*2); // 10
println((2+4)*2); // 12As you can tell from the name, these operators are able to compare one expression (which can be a number, boolean or even a string expression) to another. Returns true or false.
-
==Returns true if the the left expression has the same value as the right expression -
!=Returns true if the the left expression value differs from the right expression value -
>Returns true if the the left expression value is greater than the right expression value -
<Returns true if the the left expression value is lower than the right expression value -
>=Returns true if the the left expression value is greater than or is equal to the right expression value -
<=Returns true if the the left expression value is lower than or is equal to the right expression value
println(1 > 2); // false
println(2 == 2); // true
println("abc" < "abz"); // trueThese operators can operate with boolean values and also return a boolean.
-
||Returns true if the left OR the right expression resolves to true -
&&Returns true if the left AND the right expression resolves to true
println(1 == 1 && 2 > 3); // false (because the right expression is false)
println(1 == 1 || 2 > 3); // true (because the left expression is true)As said, object types can store objects for creating a storyboard. Currently, there is only one object type for handling both sprites (pictures) and animations. Object type variables are a little bit different than the rest of the variables, so I'm gonna explain how they are used.
Like other variables, you can declare them in this way:
Object o;But their initialization differs. To store an object, you have to first create a new object with the new keyword:
o = new Sprite(0, "test.png", Foreground, Centre);
Object anim = new Animation(1, "animation.png", Background, TopLeft, 10, 50, LoopForever);You may then perform tasks on them, such as:
o.move(0,1000,0,100,400,100);
anim.rotate(0,2000, 0, pi()*2);As this topic is kind of complex, we will discuss that in detail later on.