2.7. Expressions

[<<<] [>>>]

Up to know you have already seen some simple examples of expression. However before going on it is a wise choice to get into more details regarding them. When you see the example

A = 3

the number 3 on the right hand side is an expression. This is actually the simplest expression ever containing a single integer number. Speaking generally an expression is made of integers, real numbers and strings connected together with operators and groupped using parentheses. You can add numbers, subtract and so on. The operators that you can use are:

^    power, for example 2^3 = 8      (8)
*    multiply                        (7)
/    divide                          (7)
\    integer divide                  (7)
%    modulo operator                 (7)
+    addition                        (6)
-    substraction                    (6)
&    string concatenation            (5)
like string like operator            (4)
=    test equality                   (3)
<    test less-than                  (3)
<=   test less-then-or-equal         (3)
>    test greater-then               (3)
>=   test greater-than-or-equal      (3)
<>   test non-equality               (3)
and  bitwise AND operator            (2)
or   bitwise OR operator             (1)
xor  bitwise XOR operator            (1)

The operators have a precedence value. This value is given in parentheses above. When an expression is evaluated the higher precedence operators are evaluated before the lower precedence operators unless parentheses group the expression different. For example:

Example expression1.bas :

print 2+3*6

Result executing expression1.bas :

This is quite common in most languages. In case of using parentheses:

Example expression2.bas :

print (2+3)*6

Result executing expression2.bas :

Let's see some more complex examples of expressions:

Example expression3.bas :

FOR I=0 TO PI/2.0 STEP PI/20.0
PRINT I & " " & SIN(I) & " " & COS(I) & " " & SIN(I)^2+COS(I)^2
PRINT
NEXT I

Result executing expression3.bas :

The expression following the command PRINT is complex. It contains string concatenation as well as numeric function calls and addition operator and powering operator.


[<<<] [>>>]