[Python] 11. while 문(while statements)

2023. 11. 5. 15:17·Programming Language/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 > 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/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
  • 전체
    오늘
    어제
    • 분류 전체보기
      • Spring
        • MVC
        • DB
        • 핵심 원리
        • JPA
      • WEB
        • HTML
        • CSS
        • HTTP
        • Application
      • Computer Science
        • Network
        • Database
        • OS
        • 시스템 프로그래밍
        • 컴퓨터구조
      • Algorithm
        • Divide&Conquer
        • Sort
        • Greedy
        • DP
        • Backtracking
        • NP-Complete
        • Graph
      • Data Structure
        • 자료구조
        • C++ STL
        • Java Collection
      • 소프트웨어 공학
        • 시험 공부 정리
        • Theorem
      • Programming Language
        • Python
        • Java
        • C
        • C++
        • Rust
        • Theory
      • Unix_Linux
        • Common
      • React
      • PS
        • BOJ
        • Tip
        • 프로그래머스
        • CodeForce
      • Book Review
        • Clean Code
      • Math
        • Linear Algebra
      • AI
        • DL
        • ML
        • DA
        • Concepts
      • 우아한테크코스
        • 프리코스
      • Project Review
      • LegacyPosts
      • Android
      • Apple
        • Mac
        • IPhone
        • IPad
      • 모니터
      • Diary
  • 블로그 메뉴

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

  • 공지사항

  • 인기 글

  • 태그

  • 최근 댓글

  • 최근 글

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

티스토리툴바