티스토리 뷰

1. while 문

  • 표현식이 참인 동안 실행식을 반복
i = 0
total = 0
while i < 100:
    i = i + 1
    total = total + i

print(total)
  • continue
  • continue 키워드를 통해 다음 loop로 넘어 갈 수 있음
i = 0
total = 0
while i < 100:
    i = i + 1
    if i % 2 == 0:
        continue
    total = total + i

print(total)
  • break
  • break 키워드를 통해 loop를 탈출
import random

limit = 0.8
s = 0

while 1:
    tmp = random.random()
    if tmp > limit:
        break
    s = s + tmp

print(s)


Reference: https://docs.python.org/ko/3.6/reference/compound_stmts.html#while

'Computer Language > Python' 카테고리의 다른 글

모듈(Modules)  (0) 2018.07.30
클래스(Classes)  (0) 2018.07.21
예외 처리(Handling Exceptions)  (0) 2018.07.18
튜플과 시퀀스(Tuples and Sequences)  (0) 2018.07.17
딕셔러니(Dictionaries)  (0) 2018.07.15
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2025/12   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
글 보관함