CH01-L04 · foundation

Conditions and errors

Choose between two values and fix an error in an expression or its data.

General CELRuns hereDocumented
What will I learn?

You can use the conditional operator and distinguish syntax errors from input-data errors.

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

The conditional operator has the form condition ? valueWhenTrue : valueWhenFalse. CEL evaluates the condition first and then evaluates only the selected branch. The two branches should represent the same kind of result so that callers know what to expect.

Read the prepared example

The CEL + JSON rule is:

score >= passScore ? 'passed' : 'try again'

Its input is:

{
  "score": 72,
  "passScore": 80
}

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. 72 >= 80 returns false.
  2. CEL selects the expression after :.
  3. The final result is "try again".

Your task

  1. Run the example and confirm the selected branch.
  2. Change score to 80 and run again.
  3. Remove one quote, observe the syntax error, and restore it.

Expected observation: A score of 80 selects "passed" because >= includes equality.

Common mistake

A syntax error prevents parsing before any input can be evaluated. A missing variable parses correctly but fails when the expression reads that value.

Show the explanation

No. Only the branch selected by the condition is evaluated.

Knowledge check

Is the false branch evaluated when the condition is true?

Key takeaway

Use ?: for a value selected by one condition, and separate syntax failures from input failures.

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.