Assignment Operations: Difference between revisions

From BR Wiki
Jump to navigation Jump to search
(edit)
 
(edit)
Line 4: Line 4:


Both of them are [[Binary operators]], which means they take two arguments - one on the left, and another one on the right.
Both of them are [[Binary operators]], which means they take two arguments - one on the left, and another one on the right.
==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:
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:
Line 14: Line 16:


The disadvantage of the equal sign assignment operator '''=''' is that it may only be used as a separate statement.
The disadvantage of the equal sign assignment operator '''=''' is that it may only be used as a separate statement.
==Forced Assignment==


If your programming needs call for multiple operations in one statement, you may use the '''forced assignment''' operator ''':='''. The example below assigns the value of 5 to x and then compares the value of x (which is now 5) to the value of 2:
If your programming needs call for multiple operations in one statement, you may use the '''forced assignment''' operator ''':='''. The example below assigns the value of 5 to x and then compares the value of x (which is now 5) to the value of 2:

Revision as of 15:59, 8 January 2012

There are two assignment operators in BR: the equal sign = and the colon-equal sign :=

They are similar in how they work with a slight difference.

Both of them are Binary operators, which means they take two arguments - one on the left, and another one on the right.

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

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

00010 let 5 = x

The disadvantage of the equal sign assignment operator = is that it may only be used as a separate statement.

Forced Assignment

If your programming needs call for multiple operations in one statement, you may use the forced assignment operator :=. The example below 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 operator 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.