result <- 3 + 4 * 2 > 10 print(result)
Multiplication (*) has higher precedence than addition (+), so 4 * 2 = 8. Then 3 + 8 = 11. Comparisons have lower precedence than arithmetic operators, so finally 11 > 10 is TRUE. Output: [1] TRUE.
x <- 5 x <- x ^ 2 %% 3 print(x)
First, x ^ 2 is 5 squared = 25. Then 25 %% 3 is the remainder of 25 divided by 3, which is 1.
a <- 2 b <- 3 result <- a + b > 4 & b < 5 print(result)
First, a + b = 5. Then 5 > 4 is TRUE. Also, b < 5 is TRUE. TRUE & TRUE is TRUE.
x <- 4 result <- (x + 2) * (x - 1) / 2 print(result)
(4 + 2) = 6, (4 - 1) = 3, then 6 * 3 / 2. Since * and / have the same precedence and are left-associative, (6 * 3) / 2 = 18 / 2 = 9.
x <- 3 y <- 4 z <- 5 result <- x + y * z ^ 2 %% 7 - 1 print(result)
Precedence: ^ first (right-associative), so z ^ 2 = 25.
Then * and %% have the same precedence (left-associative), so (y * 25) %% 7 = 100 %% 7 = 2 (since 7*14=98, 100-98=2).
Then + and - (left-associative): x + 2 = 5, 5 - 1 = 4.
