Concept Flow - Assignment operators
Start
Evaluate Right Side
Assign Value to Left Variable
Variable Updated
End
The program evaluates the right side expression, then assigns the result to the variable on the left, updating its value.
x <- 5 x <- x + 3 x
| Step | Code Line | Variable | Value Before | Operation | Value After | Output |
|---|---|---|---|---|---|---|
| 1 | x <- 5 | x | undefined | Assign 5 | 5 | |
| 2 | x <- x + 3 | x | 5 | Add 3 and assign | 8 | |
| 3 | x | x | 8 | Output value | 8 | 8 |
| 4 | End | Execution stops |
| Variable | Start | After Step 1 | After Step 2 | After Step 3 |
|---|---|---|---|---|
| x | undefined | 5 | 8 | 8 |
Assignment operators in R: - Use '<-' to assign values: x <- 5 - To add and assign: x <- x + 3 - Variable must be defined before using it on right side - Right side evaluated first, then assigned - Updates variable value in place
<-.= can assign but is less preferred; == is for comparison; -> assigns in reverse direction.x == 10 is a comparison, not assignment; 10 -> x <- is invalid syntax; x =< 10 is invalid.x <- 10 correctly assigns 10 to x using preferred operator.x <- 5 y <- 3 x <- x + y print(x)
x is 5, y is 3.x <- x + y means x becomes 5 + 3 = 8.a <- 4 b <- 2 a =+ b print(a)
=+ is not a valid assignment or arithmetic operator in R.a <- a + b or a += b (not valid in base R).z after execution?x <- 2 y <- 3 z <- x z <- z * y z <- z + x
x is 2, y is 3, z is assigned value of x, so 2.z <- z * y means z = 2 * 3 = 6.z <- z + x means z = 6 + 2 = 8.Pass the test and this pattern moves unknown