Thursday, 28 July 2016

Today I started with Expressions which include Arithmatic, Relational,Logical operators.

Expressions denote values. Expressions in Lua include the numeric constants and string literals, variables, unary and binary operations, and function calls. Expressions can be also the unconventional function definitions and table constructors.

Lua supports the usual arithmetic operators: the binary `+´ (addition), `-´ (subtraction), `*´ (multiplication), `/´ (division), and the unary `-´ (negation). All of them operate on real numbers.
Lua also offers partial support for `^´ (exponentiation). One of the design goals of Lua is to have a tiny core. An exponentiation operation (implemented through the pow function in C) would mean that we should always need to link Lua with the C mathematical library. To avoid this need, the core of Lua offers only the syntax for the `^´ binary operator, which has the higher precedence among all operations. The mathematical library (which is standard, but not part of the Lua core) gives to this operator its expected meaning.

3.2 – Relational Operators

Lua provides the following relational operators:
    <   >   <=  >=  ==  ~=
All these operators always result in true or false.
The operator == tests for equality; the operator ~= is the negation of equality. We can apply both operators to any two values. If the values have different types, Lua considers them different values. Otherwise, Lua compares them according to their types. Specifically, nil is equal only to itself.
Lua compares tables, userdata, and functions by reference, that is, two such values are considered equal only if they are the very same object. For instance, after the code
    a = {}; a.x = 1; a.y = 0
    b = {}; b.x = 1; b.y = 0
    c = a
you have that a==c but a~=b.
We can apply the order operators only to two numbers or to two strings. Lua compares numbers in the usual way. Lua compares strings in alphabetical order, which follows the locale set for Lua. For instance, with the European Latin-1 locale, we have "acai" < "açaí" < "acorde". Other types can be compared only for equality (and inequality).
When comparing values with different types, you must be careful: Remember that "0"==0 is false. Moreover, 2<15 is obviously true, but "2"<"15" is false (alphabetical order!). To avoid inconsistent results, Lua raises an error when you mix strings and numbers in an order comparison, such as 2<"15".

3.3 – Logical Operators

The logical operators are andor, and not. Like control structures, all logical operators consider false and nil as false and anything else as true. The operator and returns its first argument if it is false; otherwise, it returns its second argument. The operator or returns its first argument if it is not false; otherwise, it returns its second argument:
    print(4 and 5)         --> 5
    print(nil and 13)      --> nil
    print(false and 13)    --> false
    print(4 or 5)          --> 4
    print(false or 5)      --> 5
Both and and or use short-cut evaluation, that is, they evaluate their second operand only when necessary.
A useful Lua idiom is x = x or v, which is equivalent to
    if not x then x = v end
i.e., it sets x to a default value v when x is not set (provided that x is not set to false).
Another useful idiom is (a and b) or c (or simply a and b or c, because and has a higher precedence than or), which is equivalent to the C expression
    a ? b : c
provided that b is not false. For instance, we can select the maximum of two numbers x and y with a statement like
    max = (x > y) and x or y
When x > y, the first expression of the and is true, so the and results in its second expression (x) (which is also true, because it is a number), and then the or expression results in the value of its first expression, x. When x > y is false, the and expression is false and so the or results in its second expression, y.
The operator not always returns true or false:
    print(not nil)      --> true
    print(not false)    --> true
    print(not 0)        --> false
    print(not not nil)  --> false

No comments:

Post a Comment