print('--------single else:if break--------')
for l in [1, 9, 0]:
if l % 3 == 0:
print("if")
break
else:
print("else")
print(l)
print('--------single else:no if--------')
for m in [1, 9, 0]:
print("for")
break
else:
print("else")
print(m)
print('------single else:no break--------')
for n in [1, 9, 0]:
if n % 3 == 0:
print("if")
else:
print("else")
print(n)
print('------single else:no if break--------')
for o in [1, 9, 0]:
print("for")
else:
print("else")
print(o)
print('------double else:no break--------')
for i in [1, 9, 0]:
if i % 3 == 0:
print("if")
else:
print("inner else")
else:
print("outer else")
print(i)
print('--------double else:if break--------')
for j in [1, 9, 0]:
if j % 3 == 0:
print("if")
break
else:
print("inner else")
else:
print("outer else")
print(j)
print('--------double else:else break--------')
for k in [1, 9, 0]:
if k % 3 == 0:
print("if")
else:
print("inner else")
break
else:
print("outer else")
print(k)
print('------double else:break if --------')
for p in [1, 9, 0]:
if p % 3 == 0:
if p == 2:
print("if")
break
else:
print("inner else")
else:
print("outer else")
print(p)
print('------double else:break anyway --------')
for q in [1, 9, 0]:
if q % 3 == 0:
if q == 2:
print("if")
else:
print("inner else")
break
else:
print("outer else")
print(q)
打印结果如下
--------single else:if break--------
if
--------single else:no if--------
for
------single else:no break--------
if
if
else
0
------single else:no if break--------
for
for
for
else
0
------double else:no break--------
inner else
if
if
outer else
0
--------double else:if break--------
inner else
if
--------double else:else break--------
inner else
------double else:break if --------
inner else
inner else
outer else
0
------double else:break anyway --------
inner else
Process finished with exit code 0
答案是如果触发了break则else不执行