return 會直接另函式返回,函式就執行結束了,所有該函式體內的程式碼都不再執行了,所以該函式體內的迴圈也不可能再繼續執行。
如果你需要讓迴圈繼續執行,就不能return函式,而應該選用break或者continue。
break:跳出所在的當前整個迴圈,到外層程式碼繼續執行。
continue:跳出本次迴圈,從下一個迭代繼續執行迴圈,內層迴圈執行完畢,外層程式碼繼續執行。
return:直接返回函式,所有該函式體內的程式碼(包括迴圈體)都不會再執行。
用下邊的示例程式碼來解釋:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
def return_continue_break(type):
if(not type in ["return", "continue", "break"]):
print ""type" should be "return, continue, break"."
return
for j in range(0, 10):
for i in range(0, 10):
print "j_i: %d_%d" %(j, i)
if(i > 3):
if(type == "return"):
elif(type == "continue"):
continue
else:
break
print "executed!"
if __name__ == "__main__":
return_continue_break("break")
return_continue_break("continue")
return_continue_break("return")
BREAK的輸出為:
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
j_i: 0_0
executed!
j_i: 0_1
j_i: 0_2
j_i: 0_3
j_i: 0_4
j_i: 1_0
j_i: 1_1
j_i: 1_2
j_i: 1_3
j_i: 1_4
j_i: 2_0
j_i: 2_1
j_i: 2_2
j_i: 2_3
j_i: 2_4
j_i: 3_0
j_i: 3_1
j_i: 3_2
j_i: 3_3
j_i: 3_4
j_i: 4_0
j_i: 4_1
j_i: 4_2
j_i: 4_3
j_i: 4_4
j_i: 5_0
j_i: 5_1
j_i: 5_2
j_i: 5_3
j_i: 5_4
j_i: 6_0
j_i: 6_1
j_i: 6_2
j_i: 6_3
j_i: 6_4
j_i: 7_0
j_i: 7_1
j_i: 7_2
j_i: 7_3
j_i: 7_4
j_i: 8_0
j_i: 8_1
j_i: 8_2
j_i: 8_3
j_i: 8_4
j_i: 9_0
j_i: 9_1
j_i: 9_2
j_i: 9_3
j_i: 9_4
RETURN的輸出為:
CONTINUE的輸出為:
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
j_i: 0_5
j_i: 0_6
j_i: 0_7
j_i: 0_8
j_i: 0_9
j_i: 1_5
j_i: 1_6
j_i: 1_7
j_i: 1_8
j_i: 1_9
j_i: 2_5
j_i: 2_6
j_i: 2_7
j_i: 2_8
j_i: 2_9
j_i: 3_5
j_i: 3_6
j_i: 3_7
j_i: 3_8
j_i: 3_9
j_i: 4_5
j_i: 4_6
j_i: 4_7
j_i: 4_8
j_i: 4_9
j_i: 5_5
j_i: 5_6
j_i: 5_7
j_i: 5_8
j_i: 5_9
j_i: 6_5
j_i: 6_6
j_i: 6_7
j_i: 6_8
j_i: 6_9
j_i: 7_5
j_i: 7_6
j_i: 7_7
j_i: 7_8
j_i: 7_9
j_i: 8_5
j_i: 8_6
j_i: 8_7
j_i: 8_8
j_i: 8_9
j_i: 9_5
j_i: 9_6
j_i: 9_7
j_i: 9_8
j_i: 9_9
return 會直接另函式返回,函式就執行結束了,所有該函式體內的程式碼都不再執行了,所以該函式體內的迴圈也不可能再繼續執行。
如果你需要讓迴圈繼續執行,就不能return函式,而應該選用break或者continue。
break:跳出所在的當前整個迴圈,到外層程式碼繼續執行。
continue:跳出本次迴圈,從下一個迭代繼續執行迴圈,內層迴圈執行完畢,外層程式碼繼續執行。
return:直接返回函式,所有該函式體內的程式碼(包括迴圈體)都不會再執行。
用下邊的示例程式碼來解釋:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
def return_continue_break(type):
if(not type in ["return", "continue", "break"]):
print ""type" should be "return, continue, break"."
return
for j in range(0, 10):
for i in range(0, 10):
print "j_i: %d_%d" %(j, i)
if(i > 3):
if(type == "return"):
return
elif(type == "continue"):
continue
else:
break
print "executed!"
if __name__ == "__main__":
return_continue_break("break")
return_continue_break("continue")
return_continue_break("return")
BREAK的輸出為:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
j_i: 0_0
executed!
j_i: 0_1
executed!
j_i: 0_2
executed!
j_i: 0_3
executed!
j_i: 0_4
j_i: 1_0
executed!
j_i: 1_1
executed!
j_i: 1_2
executed!
j_i: 1_3
executed!
j_i: 1_4
j_i: 2_0
executed!
j_i: 2_1
executed!
j_i: 2_2
executed!
j_i: 2_3
executed!
j_i: 2_4
j_i: 3_0
executed!
j_i: 3_1
executed!
j_i: 3_2
executed!
j_i: 3_3
executed!
j_i: 3_4
j_i: 4_0
executed!
j_i: 4_1
executed!
j_i: 4_2
executed!
j_i: 4_3
executed!
j_i: 4_4
j_i: 5_0
executed!
j_i: 5_1
executed!
j_i: 5_2
executed!
j_i: 5_3
executed!
j_i: 5_4
j_i: 6_0
executed!
j_i: 6_1
executed!
j_i: 6_2
executed!
j_i: 6_3
executed!
j_i: 6_4
j_i: 7_0
executed!
j_i: 7_1
executed!
j_i: 7_2
executed!
j_i: 7_3
executed!
j_i: 7_4
j_i: 8_0
executed!
j_i: 8_1
executed!
j_i: 8_2
executed!
j_i: 8_3
executed!
j_i: 8_4
j_i: 9_0
executed!
j_i: 9_1
executed!
j_i: 9_2
executed!
j_i: 9_3
executed!
j_i: 9_4
RETURN的輸出為:
1
2
3
4
5
6
7
8
9
j_i: 0_0
executed!
j_i: 0_1
executed!
j_i: 0_2
executed!
j_i: 0_3
executed!
j_i: 0_4
CONTINUE的輸出為:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
j_i: 0_0
executed!
j_i: 0_1
executed!
j_i: 0_2
executed!
j_i: 0_3
executed!
j_i: 0_4
j_i: 0_5
j_i: 0_6
j_i: 0_7
j_i: 0_8
j_i: 0_9
j_i: 1_0
executed!
j_i: 1_1
executed!
j_i: 1_2
executed!
j_i: 1_3
executed!
j_i: 1_4
j_i: 1_5
j_i: 1_6
j_i: 1_7
j_i: 1_8
j_i: 1_9
j_i: 2_0
executed!
j_i: 2_1
executed!
j_i: 2_2
executed!
j_i: 2_3
executed!
j_i: 2_4
j_i: 2_5
j_i: 2_6
j_i: 2_7
j_i: 2_8
j_i: 2_9
j_i: 3_0
executed!
j_i: 3_1
executed!
j_i: 3_2
executed!
j_i: 3_3
executed!
j_i: 3_4
j_i: 3_5
j_i: 3_6
j_i: 3_7
j_i: 3_8
j_i: 3_9
j_i: 4_0
executed!
j_i: 4_1
executed!
j_i: 4_2
executed!
j_i: 4_3
executed!
j_i: 4_4
j_i: 4_5
j_i: 4_6
j_i: 4_7
j_i: 4_8
j_i: 4_9
j_i: 5_0
executed!
j_i: 5_1
executed!
j_i: 5_2
executed!
j_i: 5_3
executed!
j_i: 5_4
j_i: 5_5
j_i: 5_6
j_i: 5_7
j_i: 5_8
j_i: 5_9
j_i: 6_0
executed!
j_i: 6_1
executed!
j_i: 6_2
executed!
j_i: 6_3
executed!
j_i: 6_4
j_i: 6_5
j_i: 6_6
j_i: 6_7
j_i: 6_8
j_i: 6_9
j_i: 7_0
executed!
j_i: 7_1
executed!
j_i: 7_2
executed!
j_i: 7_3
executed!
j_i: 7_4
j_i: 7_5
j_i: 7_6
j_i: 7_7
j_i: 7_8
j_i: 7_9
j_i: 8_0
executed!
j_i: 8_1
executed!
j_i: 8_2
executed!
j_i: 8_3
executed!
j_i: 8_4
j_i: 8_5
j_i: 8_6
j_i: 8_7
j_i: 8_8
j_i: 8_9
j_i: 9_0
executed!
j_i: 9_1
executed!
j_i: 9_2
executed!
j_i: 9_3
executed!
j_i: 9_4
j_i: 9_5
j_i: 9_6
j_i: 9_7
j_i: 9_8
j_i: 9_9