Files
the-way-to-go_ZH_CN/eBook/05.0.md
Haigang Zhou f5dae8f559 4-5 章 (#844)
Co-authored-by: Joe Chen <jc@unknwon.io>
2022-05-09 21:42:14 +08:00

24 lines
1.1 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 5.0 控制结构
到目前为止,我们看到的 Go 程序都是从 `main()` 函数开始执行然后按顺序执行该函数体中的代码。但我们经常会需要只有在满足一些特定情况时才执行某些代码也就是说在代码里进行条件判断。针对这种需求Go 提供了下面这些条件结构和分支结构:
- `if`-`else` 结构
- `switch` 结构
- `select` 结构,用于 channel 的选择([第 14.4 节](14.4.md)
可以使用迭代或循环结构来重复执行一次或多次某段代码(任务):
- `for` (`range`) 结构
一些如 `break``continue` 这样的关键字可以用于中途改变循环的状态。
此外,你还可以使用 `return` 来结束某个函数的执行,或使用 `goto` 和标签来调整程序的执行位置。
Go 完全省略了 `if``switch``for` 结构中条件语句两侧的括号,相比 Java、C++ 和 C# 中减少了很多视觉混乱的因素,同时也使你的代码更加简洁。
## 链接
- [目录](directory.md)
- 上一章:[指针](04.9.md)
- 下一节:[if-else 结构](05.1.md)