continue
-
13. 반복문 제어하기Python/Grammar 2022. 12. 8. 01:42
반복문을 제어하는 방법은 break와 continue를 사용하는 것이다. break를 사용해 반복문의 제어 흐름을 중단하고 루프를 빠져나오거나, continue를 사용해 반복문의 제어 흐름은 유지하되 코드 실행만 건너뛸 수 있다. # 1. break로 반복문 끝내기 1-1. while에서 break로 반복문 끝내기 i = 0 while True: # 무한 루프 생성 i += 2 if i == 8: # i가 8일 때 print(f"repeated {i // 2} times. escape the loop.") break # 반복문(무한 루프)을 끝냄 # 실행 결과 repeated 4 times. escape the loop. 1-2. for에서 break로 반복문 끝내기 for i in range(10): ..