Skip to content

Commit

Permalink
05.5
Browse files Browse the repository at this point in the history
  • Loading branch information
unknwon committed Jun 16, 2014
1 parent 2ee37bd commit 311a81f
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

## 翻译进度

5.4 [for 结构](eBook/05.4.md)
5.5 [Break 与 continue](eBook/05.5.md)

## 支持本书

Expand Down
82 changes: 70 additions & 12 deletions eBook/05.5.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,76 @@
## 啊哦,亲,你看得也太快了。。。还没翻译完呢 0 0
# 5.5 Break 与 continue

要不等到 **2014 年 6 月 10 日** 再来看看吧~~
您可以使用 break 语句重写 for2.go 的代码:

这里还有一些其它的学习资源噢~
Listing 5.10 [for3.go](examples/chapter_5/for3.go)

- [《Go编程基础》](https://github.com/Unknwon/go-fundamental-programming)
- [《Go Web编程》](https://github.com/astaxie/build-web-application-with-golang)
- [《Go名库讲解》](https://github.com/Unknwon/go-rock-libraries-showcases)
```
for {
i = i - 1
fmt.Printf(“The variable i is now: %d\n”, i)
if i < 0 {
break
}
}
```

神马?你说你不想学习?那好吧,去逛逛看看行情也行~
因此每次迭代都会对条件进行检查(i < 0),以此判断是否需要停止循环。如果退出条件满足,则使用 break 语句退出循环。

- [Go Walker](https://gowalker.org) **Go 项目 API 文档在线浏览工具**
- [Go 中国社区](http://bbs.go-china.org)
- [Go语言学习园地](http://studygolang.com/)
- [Golang中国](http://golangtc.com)
一个 break 的作用范围为该语句出现后的最内部的结构,它可以被用于任何形式的 for 循环(计数器、条件判断等)。但在 switch 或 select 语句中(详见第 13 章),break 语句的作用结果是跳过整个代码块,执行后续的代码。

# 5.5 Break 与 continue
下面的示例中包含了嵌套的循环体(for4.go),break 只会退出最内层的循环:

Listing 5.11 [for4.go](examples/chapter_5/for4.go)

```
package main
func main() {
for i:=0; i<3; i++ {
for j:=0; j<10; j++ {
if j>5 {
break
}
print(j)
}
print(" ")
}
}
```

输出:

012345 012345 012345

关键字 continue 忽略剩余的循环体而直接进入下一次循环的过程,但不是无条件执行下一次循环,执行之前依旧需要满足循环的判断条件。

Listing 5.12 [for5.go](examples/chapter_5/for5.go)

```
package main
func main() {
for i := 0; i < 10; i++ {
if i == 5 {
continue
}
print(i)
print(" ")
}
}
```

输出:

```
0 1 2 3 4 6 7 8 9
5 is skipped
```

关键字 continue 只能被用于 for 循环中。

## 链接

- [目录](directory.md)
- 上一节:[for 结构](05.4.md)
- 下一节:[标签与 goto](05.6.md)
18 changes: 18 additions & 0 deletions eBook/05.6.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
## 啊哦,亲,你看得也太快了。。。还没翻译完呢 0 0

要不等到 **2014 年 6 月 18 日** 再来看看吧~~

这里还有一些其它的学习资源噢~

- [《Go编程基础》](https://github.com/Unknwon/go-fundamental-programming)
- [《Go Web编程》](https://github.com/astaxie/build-web-application-with-golang)
- [《Go名库讲解》](https://github.com/Unknwon/go-rock-libraries-showcases)

神马?你说你不想学习?那好吧,去逛逛看看行情也行~

- [Go Walker](https://gowalker.org) **Go 项目 API 文档在线浏览工具**
- [Go 中国社区](http://bbs.go-china.org)
- [Go语言学习园地](http://studygolang.com/)
- [Golang中国](http://golangtc.com)

# 5.6 标签与 goto

0 comments on commit 311a81f

Please sign in to comment.