//=$secondUrl?>
Python
반복문 for else for문이 break없이 작동한 경우 else문 작동
반복문 for else for문이 break없이 작동한 경우 else문 작동
안녕하세요.
파이썬에서는 for문에 else문을 사용할 수 있어요.
for문이 break문 없이 작동을 다 마치면 else문이 작동합니다.
파이썬 for else문 사용하기
for else문은 다음과 같이 사용합니다.
list = [1,2,3,4,5] for i in list: print(i) else: for문이 break문 없이 작동 끝난 후 수행할 명령
그럼 코드를 한번 돌려봅시다.
list = [1,2,3,4,5] for i in list: print(i) else: print('for completed')

결과는 바로 아래에서 확인할 수 있습니다.

break문없이 for문이 작동했기 때문에 else문까지 정상적으로 작동합니다.
그럼 이번엔 for문안에 break문을 넣어볼게요.
list = [1,2,3,4,5] for i in list: if i == 5: break print(i) else: print('for completed')

결과는 바로 아래에서 확인할 수 있습니다.

5에서 break문을 만나서 else문은 작동하지 않았습니다.
//=$langList['bottomThankyou'][$langMode]?>