以前对break和continue的使用都比较模糊,今天用了个简单的例子明白了其中的用法。程序只是为了说明break和continue的区别,写的很简单。
1 #include <iostream>
2 using namespace std;
3
4 int main()
5 {
6 int a[10][10] = {0};
7 for (size_t i = 0; i != 10; ++i)
8 for (size_t j = 0; j != 10; ++j)
9 a[i][j] = i;
10
11 for (size_t i = 0; i != 10; ++i)
12 for (size_t j = 0; j != 10; ++j)
13 {
14 if (a[i][j] == 5)
15 break;
16 if (a[i][j] == 4)
17 continue;
18
19 a[i][j] = 4;
20 a[i][j] = 5;
21 }
22
23 return 0;
2 using namespace std;
3
4 int main()
5 {
6 int a[10][10] = {0};
7 for (size_t i = 0; i != 10; ++i)
8 for (size_t j = 0; j != 10; ++j)
9 a[i][j] = i;
10
11 for (size_t i = 0; i != 10; ++i)
12 for (size_t j = 0; j != 10; ++j)
13 {
14 if (a[i][j] == 5)
15 break;
16 if (a[i][j] == 4)
17 continue;
18
19 a[i][j] = 4;
20 a[i][j] = 5;
21 }
22
23 return 0;
24 }
来看看在VS2010下的第二个两重循环的反汇编代码 。
1 for (size_t i = 0; i != 10; ++i)
2 004148B3 mov dword ptr [i],0
3 004148BD jmp main+0CEh (4148CEh)
4 004148BF mov eax,dword ptr [i] ;使用break会跳转到这里
5 004148C5 add eax,1
6 004148C8 mov dword ptr [i],eax
7 004148CE cmp dword ptr [i],0Ah
8 004148D5 je main+17Fh (41497Fh)
9 for (size_t j = 0; j != 10; ++j)
10 004148DB mov dword ptr [j],0
11 004148E5 jmp main+0F6h (4148F6h)
12 004148E7 mov eax,dword ptr [j]
13 004148ED add eax,1
14 004148F0 mov dword ptr [j],eax
15 004148F6 cmp dword ptr [j],0Ah
16 004148FD je main+17Ah (41497Ah)
17 {
18 if (a[i][j] == 5)
19 004148FF mov eax,dword ptr [i]
20 00414905 imul eax,eax,28h
21 00414908 lea ecx,[ebp+eax-198h]
22 0041490F mov edx,dword ptr [j]
23 00414915 cmp dword ptr [ecx+edx*4],5
24 00414919 jne main+11Dh (41491Dh)
25 break;
26 0041491B jmp main+17Ah (41497Ah) ;这里会跳转到
27 if (a[i][j] == 4)
28 0041491D mov eax,dword ptr [i]
29 00414923 imul eax,eax,28h
30 00414926 lea ecx,[ebp+eax-198h]
31 0041492D mov edx,dword ptr [j]
32 00414933 cmp dword ptr [ecx+edx*4],4
33 00414937 jne main+13Bh (41493Bh)
34 continue;
35 00414939 jmp main+0E7h (4148E7h) ;这里也会跳转
36
37 a[i][j] = 4;
38 0041493B mov eax,dword ptr [i]
39 00414941 imul eax,eax,28h
40 00414944 lea ecx,[ebp+eax-198h]
41 0041494B mov edx,dword ptr [j]
42 00414951 mov dword ptr [ecx+edx*4],4
43 a[i][j] = 5;
44 00414958 mov eax,dword ptr [i]
45 0041495E imul eax,eax,28h
46 00414961 lea ecx,[ebp+eax-198h]
47 00414968 mov edx,dword ptr [j]
48 0041496E mov dword ptr [ecx+edx*4],5
49 }
50 00414975 jmp main+0E7h (4148E7h) ;从这两个跳转的地址处我们就可以看出两者使用的区别。
2 004148B3 mov dword ptr [i],0
3 004148BD jmp main+0CEh (4148CEh)
4 004148BF mov eax,dword ptr [i] ;使用break会跳转到这里
5 004148C5 add eax,1
6 004148C8 mov dword ptr [i],eax
7 004148CE cmp dword ptr [i],0Ah
8 004148D5 je main+17Fh (41497Fh)
9 for (size_t j = 0; j != 10; ++j)
10 004148DB mov dword ptr [j],0
11 004148E5 jmp main+0F6h (4148F6h)
12 004148E7 mov eax,dword ptr [j]
13 004148ED add eax,1
14 004148F0 mov dword ptr [j],eax
15 004148F6 cmp dword ptr [j],0Ah
16 004148FD je main+17Ah (41497Ah)
17 {
18 if (a[i][j] == 5)
19 004148FF mov eax,dword ptr [i]
20 00414905 imul eax,eax,28h
21 00414908 lea ecx,[ebp+eax-198h]
22 0041490F mov edx,dword ptr [j]
23 00414915 cmp dword ptr [ecx+edx*4],5
24 00414919 jne main+11Dh (41491Dh)
25 break;
26 0041491B jmp main+17Ah (41497Ah) ;这里会跳转到
27 if (a[i][j] == 4)
28 0041491D mov eax,dword ptr [i]
29 00414923 imul eax,eax,28h
30 00414926 lea ecx,[ebp+eax-198h]
31 0041492D mov edx,dword ptr [j]
32 00414933 cmp dword ptr [ecx+edx*4],4
33 00414937 jne main+13Bh (41493Bh)
34 continue;
35 00414939 jmp main+0E7h (4148E7h) ;这里也会跳转
36
37 a[i][j] = 4;
38 0041493B mov eax,dword ptr [i]
39 00414941 imul eax,eax,28h
40 00414944 lea ecx,[ebp+eax-198h]
41 0041494B mov edx,dword ptr [j]
42 00414951 mov dword ptr [ecx+edx*4],4
43 a[i][j] = 5;
44 00414958 mov eax,dword ptr [i]
45 0041495E imul eax,eax,28h
46 00414961 lea ecx,[ebp+eax-198h]
47 00414968 mov edx,dword ptr [j]
48 0041496E mov dword ptr [ecx+edx*4],5
49 }
50 00414975 jmp main+0E7h (4148E7h) ;从这两个跳转的地址处我们就可以看出两者使用的区别。
51 0041497A jmp main+0BFh (4148BFh)
上面的代码主要看红色部分加以理解就可以了。
得出结论:break是结束本层的循环,continue是结束本次的迭代。两者的使用情况就得具体情况具体分析了。