# 4.11 条件表达式

条件表达式就是根据一个表达式的结果，求值另两个表达式里的其中一个表达式。

让我们看一看一个例子（介绍另一个属性修饰符，`th:class`）：

```markup
<tr th:class="${row.even}? 'even' : 'odd'">
  ...
</tr>
```

一个条件表达式的三个部分（`condition`, `then` and `else`）本身也是表达式，这意味着它们也可以是变量（`${...}`, `*{...}`），信息（`#{...}`），URL（`@{...}`）或者字面量（`'...'`）。

条件表达式也可以使用括号嵌套：

```markup
<tr th:class="${row.even}? (${row.first}? 'first' : 'even') : 'odd'">
  ...
</tr>
```

Else表达式也可以省略。在这种情况下，当条件为false时，将返回null值：

```markup
<tr th:class="${row.even}? 'alt'">
  ...
</tr>
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://jack80342.gitbook.io/thymeleaf/i.-using-thymeleaf/4-standard-expression-syntax/4.11-conditional-expressions.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
