Skip to content

Commit 99ed928

Browse files
authored
Create break.md
创建文件
1 parent 1806f69 commit 99ed928

1 file changed

Lines changed: 65 additions & 0 deletions

File tree

command/break.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
break
2+
===
3+
4+
结束for,while或until循环。
5+
6+
## 概要
7+
8+
```shell
9+
break [n]
10+
```
11+
12+
## 主要用途
13+
14+
- 结束for,while或until循环,可指定退出几层循环。
15+
16+
17+
## 参数
18+
19+
n(可选):大于等于1的整数,用于指定退出几层循环。
20+
21+
## 返回值
22+
23+
返回成功除非n小于1。
24+
25+
## 例子
26+
27+
```shell
28+
# break的可选参数n缺省值为1。
29+
# 从外层for循环继续执行。
30+
for((i=3;i>0;i--)); do
31+
for((j=3;j>0;j--)); do
32+
if((j==2)); then
33+
# 换成break 1时结果一样
34+
break
35+
fi
36+
printf "%s %s\n" ${i} ${j}
37+
done
38+
done
39+
# 输出结果
40+
3 3
41+
2 3
42+
1 3
43+
```
44+
45+
```shell
46+
# 当n为2时:
47+
# 退出两层循环,结束。
48+
for((i=3;i>0;i--)); do
49+
for((j=3;j>0;j--)); do
50+
if((j==2)); then
51+
break 2
52+
fi
53+
printf "%s %s\n" ${i} ${j}
54+
done
55+
done
56+
# 输出结果
57+
3 3
58+
```
59+
60+
### 注意
61+
62+
1. 该命令是bash内建命令,相关的帮助信息请查看`help`命令。
63+
64+
65+
<!-- Linux命令行搜索引擎:https://jaywcjlove.github.io/linux-command/ -->

0 commit comments

Comments
 (0)