> For the complete documentation index, see [llms.txt](https://jack80342.gitbook.io/thymeleaf/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jack80342.gitbook.io/thymeleaf/i.-using-thymeleaf/4-standard-expression-syntax/4.11-conditional-expressions.md).

# 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>
```
