> For the complete documentation index, see [llms.txt](https://docs.1l.lol/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.1l.lol/basics/branch.md).

# 分支结构（if / switch）

### if 语句

```cpp
if (条件) {
    // 条件为真时执行
}
```

### if-else

```cpp
if (score >= 60)
    cout << "及格";
else
    cout << "不及格";
```

### 多分支 if-else if-else

```cpp
if (score >= 90)      cout << "A";
else if (score >= 80) cout << "B";
else if (score >= 60) cout << "C";
else                  cout << "D";
```

### switch 语句

用于判断某个表达式的多个**等值**情况：

```cpp
switch (op) {
    case '+': cout << a + b; break;
    case '-': cout << a - b; break;
    case '*': cout << a * b; break;
    default:  cout << "未知运算符";
}
```

要点：

* 每个 case 结束一般要 `break`，否则会**贯穿**执行下一个 case。
* switch 的判断表达式只能是整型或字符型，不能是区间。

### 常见错误

* 把 `==` 写成 `=`
* `if` 后面多加 `;`，导致分支体为空
* 条件写反，如把 `>` 写成 `<`


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

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

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.1l.lol/basics/branch.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
