Section 2.29 Worked Example: Swap
Subgoals for evaluating an assignment statement.
- Determine resultant data type of expression
- Update variable for pre operators based on side effect
- Solve arithmetic equation, operator precedence
- Is the Left Hand Side (LHS) of the assignment statement a variable? Check the data type of the value on right hand side (RHS) against data type of LHS variable.
- Update variable for post operators based on side effect
Subsection 2.29.1
You can watch this video or read through the content below it.
Consider these declarations.
int x,y,z;
x = 7;
y = 13;
Now consider this code for swapping the values of x and y. Note the need for a third variable to hold the value of x so that it is not lost when assigning the value of y into x.
z = x;
x = y;
y = z;
The diagram below shows the changes in memory after executing each of the six lines of code.
Practice Pages.
You have attempted of activities on this page.