Operations: Difference between revisions

From BR Wiki
Jump to navigation Jump to search
(edit)
(edit)
Line 1: Line 1:
==Comparison Operators==
==Comparison Operations==


{{:Comparison Operators}}
{{:Comparison Operations}}


==Assignment Operators==
==Assignment Operations==


{{:Assignment Operators}}
{{:Assignment Operations}}


==Arithmetic Operations==
==Arithmetic Operations==
Line 15: Line 15:
{{:String Operations}}
{{:String Operations}}


==Logical Operators==
==Logical Operations==


{{:Logical Operators}}
{{:Logical Operations}}


==Operator Precedence==
==Operation Precedence==


{{:Operator Precedence}}
{{:Operation Precedence}}


<noinclude>
<noinclude>
[[Category:Operators]]
[[Category:Operations]]
</noinclude>
</noinclude>

Revision as of 07:23, 11 January 2012

Comparison Operations

Below is the list of comparison operators:

Operator Meaning
= equal (assignment), may also be used for comparisons in some situations
== equal (comparison)
<> not equal
< less than
<= less than or equal to
> more than
>= more than or equal to

Example 1

Consider the following example of how comparison operations may be used in a program:

00010 ! prompt user and read first number
00020 print "Enter first integer: "
00030 input number1
00040 ! prompt user and read second number
00050 print "Enter second integer: "
00060 input number2
00070 if number1 == number2 then print "number1 is equal to number2"
00080 if number1 <> number2 then print "number1 is not equal to number2"
00090 if number1 <  number2 then print "number1 is less than number2"
00100 if number1 >  number2 then print "number1 is more than number2"
00110 if number1 <= number2 then print "number1 is less than or equal to number2"
00120 if number1 >= number2 then print "number1 is more than or equal to number2"


Example 2 - Ilusetrate the difference between == and =

To illustrate the difference between == and =, look at the following program. You will see the values passed to the function from (A==B) and (A=B) are 0 and 7. (A==B) is a *comparison* and will return 1 or 0 for True or False, while (A=B) is a *assignment* and assigns A the value of B.

Sample Program:

A=5
B=7
fn_test(A,B,(A==B))
fn_test(A,B,(A=B))
en
def fn_test(a,b,c)
  pr 'a=';a;'b=';b;'c=';c
fn

Sample Results:

a= 5 b= 7 c= 0
a= 7 b= 7 c= 7


Assignment Operations

There are two assignment operations in BR: the equal sign =, which denotes regular assignment, and the colon-equal sign :=, which denotes forced assignment.

Both of them are Binary operations, which means they take two arguments - one on the left side of the operator, and another one on the right and both may be used with Numeric and String variables.

Regular Assignment

The equal sign = simply makes the variable on the left side of it equal to the value on the right side of it. The example below assigns the value of 5 to the variable x:

00010 let x = 5 ! correct

Note that you cannot do the reverse. The example below will result in an error.

00010 let 5 = x ! incorrect

Forced Assignment

The example below forced-assigns the value of 5 to x and then compares the value of x (which is now 5) to the value of 2:

00010 if (x:=5) > 2 then print "The forced-assigned value is larger than 2"

Note that when this assignment operation is used in any expression (for example, in the condition of an IF THEN statement), parentheses must be used to clarify the order of execution. Otherwise, unexpected results may occur.

=When Used in IF Statements

When the equal sign is used in an IF Statement, it is NOT used for assignment. Instead, it is used for comparison. Consider the example below:

00010 let x = 1    ! assignment takes place
00020 let y = 1000 ! assignment takes place
00030 IF x = y THEN print "1 is equal to 1000" ! assignment DOES NOT takes place

In the above example, assignment DOES NOT takes place in line 30, instead the IF statement evaluates to FALSE and the print statement DOES NOT execute.

When the equal sign is used in an IF Statement, it is equivalent to using two equal signs ==. So the above example is identical in effect to the one below:

00010 let x = 1    ! assignment takes place
00020 let y = 1000 ! assignment takes place
00030 if x == y then print "1 is equal to 1000" ! assignment DOES NOT takes place

For clarity, it may be better to use == for comparison.


In contrast, when the forced assignment operation := is used in an IF Statement, it is used for forced assignment, not for comparison. Consider the example below:

00010 let x = 1
00020 if x:=2 then print "forced assignment as a condition of an if statement always evaluates to true"

Line 20 will change the value of x to 2 (forced assignment) and then print the statement.

Final Example

00010 let x=4
00020 print (x==5)
00030 print x

Line 10 assigns the value of 4 to X. Line 20 will compare X and 5, and return the boolean value 0, since x does not equal to 5. Line 30 will print the value 4.

00040 print x:=6

Line 40 will assign the value 6 to X, and print 6.


Arithmetic Operations

The four arithmetical operations supported by BR are addition, subtraction, multiplication and division. These operations literally correspond with their respective mathematical operators. Each of these operators may be followed by an equal sign, in which case the result of the operation is assigned to the variable on the left side of the operator:

Operator Effect
+ addition
+= addition of the left operand to the right operand followed by assignment of the result to the left operand
- subtraction
-= subtraction of the right operand from the left operand followed by assignment of the result to the left operand
* multiplication
*= multiplication of the left operand by the right operand followed by assignment of the result to the left operand
/ division
/= division of the left operand by the right operand followed by assignment of the result to the left operand
** power

Below are some examples of these operators:

00010 let a = 2
00020 let a += 3     ! this is equivalent to let a = a + 3, and the resulting value of a is 5
00030 let a *= 4     ! this is equivalent to let a = a * 4, and the resulting value of a is 20
00040 let a /= 10    ! this is equivalent to let a = a / 10, and the resulting value of a is 2
00050 let a = 2 ** 5 ! raises 2 to the 5th power, resulting in 32

All of these mathematical operators are examples of binary operations.

Now consider two more operators that use the same signs as addition and subtraction, yet each perform a different function.

The first one is unary plus +. The result of the unary plus operator is the value of its operand. The operand to the unary plus operator must be a numeric variable.

For example,

00010 let b = - 1
00020 let a = + b

The second one is unary minus -. The result of the unary minus operator is the opposite value of its operand. The operand to the unary minus operator must be a numeric variable.

For example,

00010 let b = - 1
00020 let a = - b

As a result, the value of a becomes 1, which is the opposite of -1.


String Operations

BR has one of the most powerful and easy-to-use set of methods for string manipulation in the industry. Consider the descriptions of these different methods:



Logical Operations

BR programmers rely heavily on logical operations and expressions for decision-making in their programs.



Operation Precedence

Parentheses play an important role in deciding the order of operations. Parentheses work the same way they do in math - the operations inside parentheses are carried out first.

Below is the BR operator precedence chart: