github.com/muratcelep/terraform@v1.1.0-beta2-not-internal-4/website/docs/language/expressions/operators.html.md (about)

     1  ---
     2  layout: "language"
     3  page_title: "Operators - Configuration Language"
     4  description: "Operators transform or combine expressions. Learn about arithmetic, logical, equality, and comparison operators."
     5  ---
     6  
     7  # Arithmetic and Logical Operators
     8  
     9  An _operator_ is a type of expression that transforms or combines one or more
    10  other expressions. Operators either combine two values in some way to
    11  produce a third result value, or transform a single given value to
    12  produce a single result.
    13  
    14  Operators that work on two values place an operator symbol between the two
    15  values, similar to mathematical notation: `1 + 2`. Operators that work on
    16  only one value place an operator symbol before that value, like
    17  `!true`.
    18  
    19  The Terraform language has a set of operators for both arithmetic and logic,
    20  which are similar to operators in programming languages such as JavaScript
    21  or Ruby.
    22  
    23  When multiple operators are used together in an expression, they are evaluated
    24  in the following order of operations:
    25  
    26  1. `!`, `-` (multiplication by `-1`)
    27  1. `*`, `/`, `%`
    28  1. `+`, `-` (subtraction)
    29  1. `>`, `>=`, `<`, `<=`
    30  1. `==`, `!=`
    31  1. `&&`
    32  1. `||`
    33  
    34  Use parentheses to override the default order of operations. Without
    35  parentheses, higher levels will be evaluated first, so Terraform will interpret
    36  `1 + 2 * 3` as `1 + (2 * 3)` and _not_ as `(1 + 2) * 3`.
    37  
    38  The different operators can be gathered into a few different groups with
    39  similar behavior, as described below. Each group of operators expects its
    40  given values to be of a particular type. Terraform will attempt to convert
    41  values to the required type automatically, or will produce an error message
    42  if automatic conversion is impossible.
    43  
    44  ## Arithmetic Operators
    45  
    46  The arithmetic operators all expect number values and produce number values
    47  as results:
    48  
    49  * `a + b` returns the result of adding `a` and `b` together.
    50  * `a - b` returns the result of subtracting `b` from `a`.
    51  * `a * b` returns the result of multiplying `a` and `b`.
    52  * `a / b` returns the result of dividing `a` by `b`.
    53  * `a % b` returns the remainder of dividing `a` by `b`. This operator is
    54    generally useful only when used with whole numbers.
    55  * `-a` returns the result of multiplying `a` by `-1`.
    56  
    57  Terraform supports some other less-common numeric operations as
    58  [functions](function-calls.html). For example, you can calculate exponents
    59  using
    60  [the `pow` function](/docs/language/functions/pow.html).
    61  
    62  ## Equality Operators
    63  
    64  The equality operators both take two values of any type and produce boolean
    65  values as results.
    66  
    67  * `a == b` returns `true` if `a` and `b` both have the same type and the same
    68    value, or `false` otherwise.
    69  * `a != b` is the opposite of `a == b`.
    70  
    71  Because the equality operators require both arguments to be of exactly the
    72  same type in order to decide equality, we recommend using these operators only
    73  with values of primitive types or using explicit type conversion functions
    74  to indicate which type you are intending to use for comparison.
    75  
    76  Comparisons between structural types may produce surprising results if you
    77  are not sure about the types of each of the arguments. For example,
    78  `var.list == []` may seem like it would return `true` if `var.list` were an
    79  empty list, but `[]` actually builds a value of type `tuple([])` and so the
    80  two values can never match. In this situation it's often clearer to write
    81  `length(var.list) == 0` instead.
    82  
    83  ## Comparison Operators
    84  
    85  The comparison operators all expect number values and produce boolean values
    86  as results.
    87  
    88  * `a < b` returns `true` if `a` is less than `b`, or `false` otherwise.
    89  * `a <= b` returns `true` if `a` is less than or equal to `b`, or `false`
    90    otherwise.
    91  * `a > b` returns `true` if `a` is greater than `b`, or `false` otherwise.
    92  * `a >= b` returns `true` if `a` is greater than or equal to `b`, or `false` otherwise.
    93  
    94  ## Logical Operators
    95  
    96  The logical operators all expect bool values and produce bool values as results.
    97  
    98  * `a || b` returns `true` if either `a` or `b` is `true`, or `false` if both are `false`.
    99  * `a && b` returns `true` if both `a` and `b` are `true`, or `false` if either one is `false`.
   100  * `!a` returns `true` if `a` is `false`, and `false` if `a` is `true`.
   101  
   102  Terraform does not have an operator for the "exclusive OR" operation. If you
   103  know that both operators are boolean values then exclusive OR is equivalent
   104  to the `!=` ("not equal") operator.