[Python] 11. while 문(while statements)

2023. 11. 5. 15:17·Programming Language(Sub)/Python

while문 (while statements)


while문의 기본 구조

  • 조건문이 참인 동안 문장을 반복해서 실행

    while 조건문:
        수행할 문장1
        수행할 문장2
        ...
    
    ex)
    treeHit = 0 # 나무를 찍은 횟수
    while treeHit < 10:
        treeHit += 1
        print("나무를 %d번 찍었습니다." % treeHit)
        if treeHit == 10:
            print("나무 넘어갑니다")
  • while문 강제로 빠져 나가기 (break)

    coffee = 10
    money = 5000
    while money >= 300:
        money -= 300
        if coffee == 0:
            print("커피가 다 떨어졌습니다")
            break
        else:
            print("돈을 지불했으니 커피를 줍니다")
            coffee -= 1
            print(f'앞으로 커피를 {coffee}잔 뽑을 수 있습니다.')
    
  • while문 맨 처음으로 돌아가기(continue)

    a = 0
    while a < 10:
        a += 1
        if a % 2 == 0: continue
        print(a) # 1 3 5 7 9
  • 아무런 일도 하지 않는 pass

    a = 0
    while a < 10:
        a += 1
        if a % 2 == 0: pass
        print(a) # 1 2 3 4 5 6 7 8 9 10
    • pass와 continue의 차이라면, pass는 pass 아래의 명령 또한 실행하지만 continue의 경우 continue 아래의 명령은 실행하지 않고 while문 맨 처음으로 돌아간다.

참조) Do it! 점프 투 파이썬 (박응용 지음)

'Programming Language(Sub) > Python' 카테고리의 다른 글

[Python] 13. 함수(function)  (0) 2023.11.05
[Python] 12. for 문(for statements)  (0) 2023.11.05
[Python] 10. if 문(if statements)  (0) 2023.11.05
[Python Advanced] Object(객체) 저장 구조에 대한 탐구  (0) 2023.11.05
[Python] 09. 변수(Variable)  (0) 2023.11.05
'Programming Language(Sub)/Python' 카테고리의 다른 글
  • [Python] 13. 함수(function)
  • [Python] 12. for 문(for statements)
  • [Python] 10. if 문(if statements)
  • [Python Advanced] Object(객체) 저장 구조에 대한 탐구
lumana
lumana
배움을 나누는 공간 https://github.com/bebeis
  • lumana
    Brute force Study
    lumana
  • 전체
    오늘
    어제
    • 분류 전체보기 (456)
      • Software Development (27)
        • Performance (0)
        • TroubleShooting (1)
        • Refactoring (0)
        • Test (8)
        • Code Style, Convetion (0)
        • DDD (0)
        • Software Engineering (18)
      • Java (71)
        • Basic (5)
        • Core (21)
        • Collection (7)
        • 멀티스레드&동시성 (13)
        • IO, Network (8)
        • Reflection, Annotation (3)
        • Modern Java(8~) (12)
        • JVM (2)
      • Spring (53)
        • Framework (12)
        • MVC (23)
        • Transaction (3)
        • AOP (11)
        • Boot (0)
        • AI (0)
      • DB Access (1)
        • Jdbc (1)
        • JdbcTemplate (0)
        • JPA (14)
        • Spring Data JPA (0)
        • QueryDSL (0)
      • Computer Science (129)
        • Data Structure (27)
        • OS (14)
        • Database (10)
        • Network (21)
        • 컴퓨터구조 (5)
        • 시스템 프로그래밍 (23)
        • Algorithm (29)
      • HTTP (8)
      • Infra (1)
        • Docker (1)
      • 프로그래밍언어론 (15)
      • Programming Language(Sub) (77)
        • Kotlin (1)
        • Python (25)
        • C++ (51)
        • JavaScript (0)
      • FE (11)
        • HTML (1)
        • CSS (9)
        • React (0)
        • Application (1)
      • Unix_Linux (0)
        • Common (0)
      • PS (13)
        • BOJ (7)
        • Tip (3)
        • 프로그래머스 (0)
        • CodeForce (0)
      • Book Review (4)
        • Clean Code (4)
      • Math (3)
        • Linear Algebra (3)
      • AI (7)
        • DL (0)
        • ML (0)
        • DA (0)
        • Concepts (7)
      • 프리코스 (4)
      • Project Review (6)
      • LegacyPosts (11)
      • 모니터 (0)
      • Diary (0)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.4
lumana
[Python] 11. while 문(while statements)
상단으로

티스토리툴바