github.com/iaas-resource-provision/iaas-rpc@v1.0.7-0.20211021023331-ed21f798c408/website/docs/language/expressions/operators.html.md (about)

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