> LeekScript Tutorial
Now let's talk about operators, which will quickly become indispensable, believe me.
And for that, we're going to do a bit of math. Don't panic!! Stay right here! Don't worry, you'll only need the basic operations: addition, subtraction, multiplication, division and modulo. You may not know that last one if you're a beginner, but it isn't very complicated.
We'll also look at operators that assign a value to a variable in other ways than with a simple "="
Your leek is very good at mental math. Let's warm it up a bit with some simple calculations.
The symbol for addition in LeekScript is "+".
To use it, write an addition, just as you've surely done before. (Well, we hope so.)
Remember that you assign a value to a variable like this:
var anInteger = 6 debug(anInteger); // Displays: 6
To do an addition, simply put your mathematical expression in place of that value.
var addition = 3 + 5; debug(addition) // Displays: 8
And there you go! Your leek just did its first addition and gives you the result. You can also pass the calculation directly as a parameter to the debug function.
debug(3+5); // Displays: 8
Unsurprisingly, the symbol for subtraction is "-"
var subtraction = 7 - 4; debug(subtraction); //Displays: 3
For this operation, don't try to use the letter "x" to mean "times". In programming, we use the "\*" symbol for multiplication
var multiplication = 3 * 5; debug(multiplication); // Displays: 15
As for division, we use the "/" symbol
var division = 20 / 4; debug(division);
A quick reminder about modulo. Our math-loving friends have what they call "Euclidean division".
Regular division of 7 by 2 gives 3.5. Euclidean division splits the result into a "quotient" and a "remainder". So we have 7 = 2 * 3 + 1. The result is a quotient of 3 and a remainder of 1.
And it's this remainder we're interested in here. Modulo is the operation that gives us the remainder of the Euclidean division. Its symbol is "%"
For example, 30%7 gives 2, since 30 = 7 * 4 + 2
var modulo = 42 % 9; debug(modulo) // Displays: 6
This division returns the quotient of the Euclidean division of two numbers. Its symbol is "\\".
Unlike modulo, which takes decimal numbers into account, integer division drops their decimal part. So 5.9 \ 2.1 becomes 5 \ 2. The result is 2.
var quotient = 30 \ 7; debug(quotient) // Displays: 4
Just like in math, operations have an order of priority.
So multiplication (\), division (/) and modulo (%) take priority over addition (+) and subtraction (-). For operators with the same priority (\, / and %, or + and -), the expression is evaluated from left to right.
If you need to change the priority in a calculation, use parentheses. For example
debug(2 + 3 * 4); // Displays: 14 debug((2 + 3) * 4); // Displays: 20
debug(12 / 2 * 3); // Displays: 18 debug(12 / (2 * 3)); // Displays: 2
You already know "=", which assigns a value to a variable:
var myVariable = 8; debug(myVariable); // Displays: 8
This operator has some big brothers. Let's say you want to add a value to a variable. Our variable is 8 and we'd like to add 3 to it. We can do it like this:
var myVariable = 8; myVariable = myVariable + 3; debug(myVariable); // Displays: 11
That works just fine, but we can do even better. You can merge the arithmetic operators with the "=" sign. That gives these 5 very handy operators: "+=", "-=", "\*=", "/=" and "%="
The previous example becomes:
var myVariable = 8; myVariable +=3; // Equivalent to myVariable = myVariable + 3; debug(myVariable); // Displays: 11
Same result, but a little less repetitive.
var myVariable = 9; myVariable += 3; // 12 myVariable -= 2; // 10 myVariable *= 4; // 40 myVariable /= 2; // 20 myVariable %= 6; // 2 debug(myVariable); // Displays: 2
Here are two very handy little operators: the increment operator "++" and the decrement operator "--".
Incrementing a variable means increasing its value by 1.
var myVariable = 3; myVariable++; // Equivalent to myVariable = myVariable + 1; debug(myVariable); // Displays: 4
And decrementing a variable means decreasing its value by 1
var myVariable = 3; myVariable--; // Equivalent to myVariable = myVariable - 1; debug(myVariable); // Displays: 2
Logical operators return a logical value from other logical values:
a && b or a and b returns true if a is true AND b is truea || b or a or b returns true if a is true OR b is true!a or not a returns true only if a is false.See also: https://leekwars.com/encyclopedia/en/Conditions_in_depth#Logical_operators
Binary operators (also called bitwise operators) are less well known than the usual math operators (such as +,-,/, ...), but experienced farmers use them to build functions or AIs that cost very few operations.
To understand how to use these operators, you need to be familiar with the binary notation (or base 2 notation) of numbers. If it's a topic you don't know, the internet is full of explanations, tutorials and reference material that will quickly get you up to speed.
If the equality 9 = 1001 leaves you puzzled, you'd better read up on binary notation before going any further.
In the rest of this chapter, we'll regularly switch between numbers written in binary and numbers written in base 10. To keep things clear and avoid confusion, we'll use the following notation:
So, for example, the following statements are all true:
5d === 101b // Converting everything to decimal notation, we do get 5d === 5d 10d > 101b // In decimal notation, this gives 10d > 5d 11d !== 11b // Still in decimal notation, 11d !== 3d
Binary operators are math operators (+, -, /, ...) like any other. They apply to 2 numeric operands and return a result that is also numeric. Like the usual math operators, they go between the two operands.
You can use these operators in expressions (see the next section), in variable assignments... in short, just like the usual math operators!
For example:
debug(3 + 4); // Will display 7 in the debugger debug(3 & 4); // Will display 0 in the debugger (the calculation is explained below)
Binary operators work "bit by bit": they take the binary notation of the numbers you give them and perform an operation on each pair of bits of the two operands. Before going into the details of how that works, though, let's first look at how each of these operators behaves on a single bit.
Important note: binary operators have names (and sometimes even LeekScript notations) very similar to those of the logical operators (and, or, ...). Keep in mind that:
So be careful not to confuse the binary AND operator, written "&", with the logical AND operator, written " && " or simply "and" in a script.
The AND operator: & This operator, written & in LeekScript, applied to a pair of bits, evaluates to 1 if both input bits are 1. It evaluates to 0 in every other case. As a truth table, this gives:
a | b | | a & b --|---|----|------ 0 | 0 | -> | 0 0 | 1 | -> | 0 1 | 0 | -> | 0 1 | 1 | -> | 1
The OR operator: | The OR operator, written "|" (Shift + backslash on a US keyboard) in LeekScript, evaluates to 1 if at least one of the two input bits is 1, and to 0 if both input bits are 0.
As a truth table: a | b | | a \| b --|---|----|------ 0 | 0 | -> | 0 0 | 1 | -> | 1 1 | 0 | -> | 1 1 | 1 | -> | 1
The exclusive OR (XOR) operator: ^ The XOR operator, written "^" in LeekScript, evaluates to 1 if exactly one of the two input bits is 1, and to 0 otherwise.
Truth table: a | b | | a ^ b --|---|----|------ 0 | 0 | -> | 0 0 | 1 | -> | 1 1 | 0 | -> | 1 1 | 1 | -> | 0
The NOT operator: ~ The NOT operator, written "~" in LeekScript, flips every 0 and 1 in your binary number.
Truth table: a | | ~a --|----|------ 0 | -> | 1 1 | -> | 0
Be careful, though: in LeekScript, integer variables are stored on 32 bits. Applying NOT to the number 10d (1010 in binary) won't give you 5d (0101b), but -11, i.e. 1111111111111111 1111111111110101 in binary.
"Okay, great! So we've got the description of something completely redundant with the logical operators!"
Not quite. We've seen how these operators behave when given a pair of bits. However, as mentioned above, these operators apply to numbers.
And as their name suggests, these operators work in the wonderful world of binary notation.
Quite simply, they go through the binary form of the two input numbers, compute a number in binary, then output the result as a number.
An example is worth a thousand words, so here's how the expression "10 & 9" is evaluated:
| | Decimal notation | | Binary notation ---------------------------|-------------------|----|------------------ Operand 1 | 10d | -> | 1010b Operand 2 | 9d | -> | 1001b Applying the operator | | | 1000b Decimal conversion | 8d | | 1000b
If we break down how we got from 1010b & 1001b to 1000b, we simply applied the "&" operator to each digit of the two numbers:
Number | Digit 1 | Digit 2 | Digit 3 | Digit 4 ------------|---------|---------|---------|-------- 10d / 1010b | 1 | 0 | 1 | 0 9d / 1001b | 1 | 0 | 0 | 1 Result | 1 | 0 | 0 | 0
So we get the following behavior:
debug(10 & 9); // Will display 8 in the debugger.
If the two numbers don't have the same number of digits in binary, the smaller one is padded with "leading" zeros before its first 1.
For example, the expression 14 ^ 3 leads to the following calculation: | | Decimal notation | | Binary notation ---------------------------|-------------------|----|------------------ Operand 1 | 14d | -> | 1110b Operand 2 | 3d | -> | 0011b Applying the operator | | | 1101b Decimal conversion | 13d | | 1101b
So we get:
debug(14 ^ 3); // Will display 13 in the debugger.
Bit shifting operators shift the bits of a number by a given amount. They are written with the symbols >> and **> operator shifts all the bits of the number to the right and inserts a 0 on the left if the number is positive, or a 1 if it is negative. This is equivalent to dividing the number by 2.
Examples with >> (5-bit numbers) Operation | Binary representation | Result | Binary representation ----------|-------------------------|----------|---------------------- 10d >> 1 | 01010b >> 1 | 5d | 00101b 10d >> 2 | 01010b >> 2 | 2d | 00010b 10d >> 3 | 01010b >> 3 | 1d | 00001b -5d >> 3 | 11011b >> 3 | -1d | 11111b
Examples with >>> (5-bit numbers) Operation | Binary representation | Result | Binary representation ----------|-------------------------|----------|---------------------- 10d >>> 3 | 01010b >>> 3 | 1d | 00001b -5d >>> 3 | 11011b >>> 3 | 3d | 00011b
**Examples with >= 1;||3|00011b| myVariable >>= 1;||6|00110b|
The big advantage of binary operators is that they cost very few operations. They make it possible to write complex functions at a reasonable cost, or to keep down the cost of a function you want to use heavily by placing it deep inside several nested loops.
You now have the basics to use these new operators. With a little imagination, you can definitely see what they could be used for...
So what is an expression? If I say "8x - 5", that's a "mathematical expression". Well, in programming, it's pretty much the same thing.
What we've been writing in this chapter are expressions. When you write:
You assign the result of the expression "8 * 9 + 4 * 2" to the variable "myVariable". The computer understands this expression and works out that its result is 80. It then assigns the value 80 to the variable.
Remember this word, we'll come back to it.
But an expression isn't just a string of numbers and symbols. Your expressions can also contain other variables!
You can perfectly well write:
Likewise, be aware that when you assign a value to a variable, any value it already held is erased. For example:
Keep these ideas in mind when you read code.
Exercises are a very important part of the tutorial, so I recommend doing every single one of them: practice makes perfect, after all. This chapter has most likely seemed simple to you, but in programming the smallest detail matters. So here's the first exercise:
SWAP exercise
Let's start easy: you have to swap the values of two variables. Your turn :) If that's still too easy, have you tried it without using a third variable?
We've almost covered all the operators. The comparison operators are still left to discover. We'll talk about them in a later chapter, since they're closely tied to conditions.
You now know how to do calculations in LeekScript. It may not seem very useful yet, but in the next chapters you'll see just how important it is to know how to do calculations.
They will cover conditions and loops, two extremely important concepts in programming.
Impossible de charger les données du jeu.
Vérifiez votre connexion et réessayez.