Difference between revisions of "W1038 L-Values and R-Values"

From Coder Merlin
(Created page with "{{ComingSoon|L-Values and R-Values}}")
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
{{ComingSoon|L-Values and R-Values}}
 
== L-Values and R-Values ==
An '''L-Value''' refers to an object that persists beyond a single expression, that is, it is addressble. The name comes from the typical manner in which assignment statements are written in most programming languages, with the variable receiving the value on the left:
<pre>
x=5+4
</pre>
In this case, the variable "x" is an L-value which is being assigned the value 5+4, or 9. "x" refers to a location in memory which will contain the value 9, and that location will continue to persist after execution of this statement completes. For example, the next statement may again refer to "x", as in:
<pre>
print(x)
</pre>
Note that in most computer languages, the variable to be assigned is on the left of the assignment operator. In these cases, it would not be possible to assign to a variable on the right, as in:
<pre>
5+4=x
</pre>
An '''R-Value''' refers to some object which does not persist after execution of the statement, that is, it is a temporary value. Think back to our original statement:
<pre>
x=5+4
</pre>
In this case, 5+4 is an R-Value. It isn’t possible to assign a value (that is, there is no persistent storage associated with) 5+4.

Revision as of 20:34, 2 December 2019

Within these castle walls be forged Mavens of Computer Science ...
— Merlin, The Coder

L-Values and R-Values[edit]

An L-Value refers to an object that persists beyond a single expression, that is, it is addressble. The name comes from the typical manner in which assignment statements are written in most programming languages, with the variable receiving the value on the left:

x=5+4

In this case, the variable "x" is an L-value which is being assigned the value 5+4, or 9. "x" refers to a location in memory which will contain the value 9, and that location will continue to persist after execution of this statement completes. For example, the next statement may again refer to "x", as in:

print(x)

Note that in most computer languages, the variable to be assigned is on the left of the assignment operator. In these cases, it would not be possible to assign to a variable on the right, as in:

5+4=x

An R-Value refers to some object which does not persist after execution of the statement, that is, it is a temporary value. Think back to our original statement:

x=5+4

In this case, 5+4 is an R-Value. It isn’t possible to assign a value (that is, there is no persistent storage associated with) 5+4.