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

From Coder Merlin
Tag: Replaced
 
Line 1: Line 1:


== L-Values and R-Values ==
{{MovedToMoodle|CS-151,Unit III,Chapter 8}}
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.

Latest revision as of 23:18, 8 February 2024

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


HintIcon.png
Moved to Moodle logo.svg
CS-151,Unit III,Chapter 8