Operations of Comparison / Logical Operations
Like every programming language as well, LUA also have operators for concatenating parameter or variables. There are arithmetic, relational and logical operators, which can be combined with different data types. For sure, all operators in LUA need two parameters or variables to compare. With the exception of minus '-' as negation of a number and the reserved word 'not'.
Comparisons are used very often. Is something equal, bigger or smaller than etc.
The result of a comparison is either true or false.
Operator | Description | Example | Result |
---|---|---|---|
== | left side equal to the right side? | "Willi" == "willi" | false |
~= | left side not equal to the right side? | "Willi" ~= "willi" | true |
< | left side smaller than the right side? | 2 < 3 | true |
> | left side bigger than the right side? | 2 > 3 | false |
<= | left side smaller or equal to right side? | 2 <= 3 | true |
>= | left side bigger or equal to right side? | 2 >= 3 | false |
There we have the two ==. They are used together as operator for comparison.
Those operators are checking a condition and as result they return true or false.
We will need these operators very often by programming in LUA so we do not need have to learn them in detail. Furthermore, they are almost self-explanatory.
As mentioned above there we have the word "not". What is it about? With this question we come to the logical operators.
LUA knows following logical operators
and, or and not
and or
With 'and' and 'or' we can combine conditions (as shown above)
The result of such a combination will be true or false as well.
That means:
if condition1 and condition2 have to be fulfiled then
result = condition1 and condition2
We use following variables
willyIsDrunken = true
grandmaIsBad = false
WilliesWifeIsBad = true
Know we combine
willyIsDrunken and grandmaIsBad --> false
if condition1 or condition2 have to be fulfiled then:
result = condition1 or condition2
willyIsDrunken or grandmaIsBad --> true
We also can use 'and' and 'or' together with 'not' (see below).
At this moment this would go beyond the scope of this chapter. We will talk about this later on.
not
not is simply a negation of a condition. It will be inverted. True becomes false and false becomes true.
e.g. (5 == 5) the result is true. At the condition
not (5==5) we get false as result.
There are a lot of more applications for this but I do not know if need them right at this moment. As mentioned before and before.....we get to it later on.
Something for our editor:
print (5==4) --> false in the output window
print (5==5) --> true in the output window
(press the arrow or F5 or Run - Run Script)
We have already learned a lot and will do an exercise with the help of our editor. One advantage is, that we will get more and more familiar with the editor. Another advantage is that we can varify what we have learned until now and proof that we have understood. Practice makes perfect. The following chapters will become more difficult. If we have struggling with these things later on it would become at least very difficult to understand new things about LUA.
Therefore, have a break and exercise.
Do you have learned enough? Can we go on? OK, but not with mathematics as we would like to have an alternation as well. Let us learn something about strings.
Copying of any content of this site (text or graphics) is not allowed, excepted any source code shown in this tutorial. See also: ....Disclaimer
Copyright © Robert Schmitz 2006
Copyright © Robert Schmitz 2006