Assignment Operations: Difference between revisions
(edit) |
No edit summary |
||
(9 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
There are two assignment | 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]] [[Variable|variables]]. | |||
Both of them are [[Binary | |||
[[Numeric]] and [[String]] [[Variable|variables]] | |||
===Regular Assignment=== | ===Regular Assignment=== | ||
Line 16: | Line 12: | ||
00010 let 5 = x ! incorrect | 00010 let 5 = x ! incorrect | ||
===Forced Assignment=== | ===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" | 00010 if (x:=5) > 2 then print "The forced-assigned value is larger than 2" | ||
'''Note''' that when this assignment | '''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 | 00010 let x = 1 ! assignment takes place | ||
00020 let y = 1000 ! assignment takes place | 00020 let y = 1000 ! assignment takes place | ||
00030 | 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, instead the IF statement evaluates to FALSE and the '''print''' statement DOES NOT execute. | 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 | 00010 let x = 1 ! assignment takes place | ||
Line 46: | Line 40: | ||
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 | 00010 let x = 1 | ||
00020 if x:=2 then print "forced assignment as a condition of an if statement always evaluates to true" | 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. | |||
<noinclude> | <noinclude> | ||
[[Category: | [[Category:operations]] | ||
[[Category:Assignment operations]] | |||
</noinclude> | </noinclude> |
Latest revision as of 00:58, 2 July 2015
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.