基本上是for/while关键字加上条件,然后循环体内写代码,代码内可能包含了必要的break或者continue语句来跳出循环或者跳过当此循环。而循环之后再则是其他语句,与前边的循环无直接关系
我们再来看python语言,在Python中的while或者for循环之后还可以有else子句,形如下:
for x in range(1,5):
if x == 6 :
print "found the number",x
break;
else:
print "not found!"
保存上边的代码,运行得到“not found”,WHY?我们先来看看python官方文档中的解释,原文在这儿:
Loop statements may have an else clause; it is executed when the loop terminates through exhaustion of the list (with for) or when the condition becomes false (with while), but not when the loop is terminated by a break statement.
翻译:循环语句后可以有一个else从句,这个else从句在因为for循环中list全部遍历完或者因为while循环中条件不满足而执行,而如果循环中有break语句执行了则else从句就不执行了。。
简单理解来:for循环中if条件一直不满足,则最后就执行else语句
我们这里来简单想象下如果用c语言式的写法,即添加found flag
found = false
for x in range(1,5):
if x == 6:
found = True
print "found the number",x
break;
if !found:
print "nout found!"