break
>>> L = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> for i in L:
if i>5:
break
print("Item: {0}".format(i))
Item: 1
Item: 2
Item: 3
Item: 4
Item: 5
continue
>>> for i in L:
if i%2 == 0:
continue
print("Item: {0}".format(i))
Item: 1
Item: 3
Item: 5
Item: 7
Item: 9
else블록이 수행되는 예제
break으로 루프가 종료되지 않은 경우 else 수행
L=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
for i in L:
if i%2 == 0:
continue
print("Item: {0}".format(i))
else:
print("Exit without break.")
print("Always this is printed")
Item: 1
Item: 3
Item: 5
Item: 7
Item: 9
Exit without break.
Always this is printed
else블록이 수행되지 않는 예제
break로 루프가 종료되는 경우 else를 수행
L=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
for i in L:
if i > 5:
break
print("Item: {0}".format(i))
else:
print("Exit without break.")
print("Always this is printed")
Item: 1
Item: 2
Item: 3
Item: 4
Item: 5
Always this is printed
'컴퓨터 > Python' 카테고리의 다른 글
클래스 선언 (0) | 2013.07.15 |
---|---|
제어문과 연관된 유용한 함수 (0) | 2013.07.12 |
while 문, for 문 (0) | 2013.07.09 |
단축평가 (0) | 2013.07.09 |
조건식의 참/거짓 판단 (0) | 2013.07.09 |