CH02-L04 · foundation

Precedence and parentheses

Change evaluation order with parentheses and compare two different results.

General CELRuns hereDocumented
What will I learn?

You can use parentheses to state the intended order of operations clearly.

Where will I try it?

Directly below, in the browser playground.

What do I do?

Run both modes, compare where their values come from, then complete the task.

Try CEL · browser pilot

Try both ways to supply values

Define local values inside one CEL expression, or provide variables as JSON input. Everything runs in your browser.

CEL pilot subset
One self-contained CEL expressioncel.bind() gives a local name to a value and returns the result of the final expression.

cel.bind()cel.bind() is an optional official extension. It is not part of the core CEL language and may not be enabled by every host application.

Tip: press Ctrl/⌘ + Enter to run.
Result

Press Run CEL to evaluate the active example.

Pilot scope: literals, explicit conversions, local cel.bind() variables, JSON variables, lists, maps, presence, collection macros, core string checks, time values, arithmetic, comparisons, logic, and the conditional operator.

What you will do

You will run the prepared expression in the playground above, change one input at a time, and explain the result. Start in CEL only, then repeat the same rule in CEL + JSON.

How it works

Operator precedence defines which operation runs first, but parentheses make the intended grouping visible to the reader. (subtotal - discount) * taxRate applies the discount before multiplication. subtotal - discount * taxRate multiplies first and therefore expresses a different calculation.

Read the prepared example

The CEL + JSON rule is:

(subtotal - discount) * taxRate

Its input is:

{
  "subtotal": 100,
  "discount": 10,
  "taxRate": 1.2
}

The CEL only tab contains the values and the rule in one expression. The enabled cel.bind() extension keeps each name local to its final argument.

Evaluation step by step

  1. Parentheses produce 100.0 - 10.0 = 90.0.
  2. 90.0 * 1.2 produces 108.0.
  3. The expression returns the calculated total.

Your task

  1. Run the original expression.
  2. Remove the parentheses and run subtotal - discount * taxRate.
  3. Explain why the second result is 88.0, then restore the original.

Expected observation: The original returns 108.0; the unparenthesized alternative returns 88.0.

Common mistake

Do not add parentheses randomly. Group operations according to the business calculation you can state in plain language.

Show the explanation

The subtraction inside parentheses runs first.

Knowledge check

Which operation runs first in the original expression?

Key takeaway

Use parentheses to communicate the calculation, not merely to satisfy the parser.

Sources

  • CEL-DEV — official CEL overview.
  • CEL-LANG — official CEL language definition.
  • CEL-GO-BIND — official documentation for the optional cel.bind() extension used by the self-contained tab.