Subtraction: 7
Division: 4.5
Floor Division: 4
Modulo: 1
Power: 81
Operators are essential for performing various operations on variables and values. You may want to multiply two numbers or compare them to know which one is greater, and operators allow us to do it.
Arithmetic operators can be used with numerical variables such as int
and float
.
Description | Operator | Example |
---|---|---|
Addition | + | a + b |
Subtraction | - | a - b |
Multiplication | * | a * b |
Division | / | a / b |
Floor Division | %/% | a %/% b |
Modulo | %% | a %% b |
Exponentiation | ^ | a ^ b |
<- 9
a <- 2
b
cat('Subtraction:', a - b)
cat('Division:', a / b)
cat('Floor Division:', a %/% b)
cat('Modulo:', a %% b)
cat('Power:', a ^ b)
Subtraction: 7
Division: 4.5
Floor Division: 4
Modulo: 1
Power: 81
We can compare two variables (or a variable and a value) using comparison operators. They are applicable to numerical variables, strings and booleans.
Description | Operator | Example |
---|---|---|
Equal To | == | a == b |
Not Equal To | != | a != b |
Greater Than | > | a > b |
Less Than | < | a < b |
Greater Than or Equal To | >= | a >= b |
Less Than or Equal To | <= | a <= b |
<- 5
a <- 2
b
cat("Equal To:", a == b)
cat("Less Than:", a > b)
cat("Not Equal To:", a != b)
cat("Greater Than:", a < b)
Equal To: FALSE
Less Than: TRUE
Not Equal To: TRUE
Greater Than: FALSE
Logical operators are used to combine conditional statements or negate them, and can only be applied to booleans.
Description | Operator | Example |
---|---|---|
Logical AND (element-wise) | & | (a == b) & (a > c) |
Logical AND | && | (a == b) && (a > c) |
Logical OR (element-wise) | | | (a > b) | (a < c) |
Logical OR | || | (a > b) || (a < c) |
Logical NOT | ! | !(a > b) |
<- 5
a <- 2
b <- 4
c
cat("(a==b) AND (a>c)", (a==b) & (a>c))
cat("(a>b) OR (a<c)", (a>b) | (a<c))
cat("NOT (a>b):", !(a>b))
(a==b) AND (a>c): FALSE
(a>b) OR (a<c): TRUE
NOT (a>b): FALSE