Logical expressions are those expressions that give a logical (or Boolean) result – i.e. TRUE or FALSE where TRUE is represented as “1” and false is represented as “0”.
Logical operators include the and, or, not statements (and their symbolic equivalents: &&, ||, !) as well as the comparison operators (greater than, less than, greater than or equal to, less than or equal to , equals, string equals, string not equal).
The language allows for ‘C’ style logical and comparison symbols as well as additional TE language equivalents where appropriate. It is up to the programmer whether to use the ‘C’ style symbols or to adopt the TE language versions depending upon personal preference. The ‘C’ style symbols were included in the language since these symbols are well known and used by a large number of programmers and it is often difficult to remember to swap to the TE language operators causing an annoying number of syntax errors to be introduced to the code by the absent minded programmer (i.e. the author).
The list of Logical and comparison operators is as follows:
‘
|
‘C’ style operator |
TE Language equivalent |
Description |
|
&& |
and |
Logical AND |
|
|| |
or |
Logical OR |
|
! |
not |
Logical NOT |
|
> |
> |
Greater Than |
|
>= |
>= |
Greater or equal |
|
< |
< |
Less Than |
|
<= |
<= |
Less or equal |
|
== |
eq |
Equals |
|
!= |
<> |
Not equal |
|
n/a |
streq |
String equals |
|
n/a |
strneq |
String not equal |
The ‘C’ style operators may be useful for ‘C’ programmers who are used to using these, but there are certain disadvantages. The first is that the symbolic nature of all the symbols makes the code more difficult to read and ends up looking like some kind of alien hyroglyphics:
if((!a && !b) || c==d)
is harder to read than:
if((not a and not b) or c eq d)
Also the ‘C’ style equals operator (==) is easy to mix up with the assignment operator (=). Statements like the following are common causes of bugs in C programs:
// This conditional expression is always true
// This is a bug if ‘if(a==1)’ was what was meant..
if(a=1) ...
The use of eq rather than == reduces the chance of introducing this kind of bug. Therefore for beginners or non C programmers it might be advisable to use the TE language versions in preference to the ‘C’ style versions if possible, but it is purely down to programmer preference.
Also notice that in the TE language there are two additional comparison operators: streq and strneq. All the other comparison operators carry out a numeric comparison of the terms, whereas streq (string equals) and strneq (string not equal) carry out a string comparison. There is a subtle difference between being equal as a number and equal as a string. A string is converted to a number by taking all the characters up to the first non-digit. So, "123", "0123", and "123ABC" are all equal numerically but are different when compared as strings.
This is especially important to remember when making checking for DTMF input using the pound or star keys:
"*" eq "*" // This gives True
"#" eq "*" // This also gives True!
because we are comparing zero with zero. Be sure to use streq, not eq, when comparing non-numeric values.
Logical conditions can include arithmetic operators and parentheses just like other expressions. In fact, there is no distinction between logical conditions and other expressions except to convert the final result to a logical value (“1” or “0”) rather than a numerical value. The following are all valid expressions:
a=123 + (5 > 3); // a will be set to 124 since (5 > 3) evaluates to TRUE ("1")
a=1000*(not 234) // a will be set to 0 since (not 234) evaluates to FALSE ("0")
However logical conditions are normally used in if-statements and to control for, while and do..until loops.