> For the complete documentation index, see [llms.txt](https://nmdkims-organization.gitbook.io/python-hand-book/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://nmdkims-organization.gitbook.io/python-hand-book/day1./2.-if-else-for/while-for-loop.md).

# while, for loop 반복

while, for loop 반복문에 대한 내용입니다.

{% hint style="info" %}
**while, for loop 반복문**

```
while 조건문:
```

```
    수행할 문장
```

{% endhint %}

```python
open = 0

while open < 10:
    open += 1
    print(f"{open}번 상자를 열었습니다.")
    if open == 10:
        print("모든 상자를 열었습니다.")

```

{% hint style="info" %}

```
위의 문장은 open이 10보다 작으면 계속 반복합니다.
```

```
아래 수행하는 문장을 보면 open에 값을 계속 1씩 증가시키고 
```

```
open 10이 되면 모든 상자를 열었다는 문장을 출력 후 open이 10이되어
```

```
open이 10보다 작다는 조건이 False가 되어 while문에서 빠져나옵니다.
```

{% endhint %}

{% hint style="info" %}

```
while문의 break continue
```

```
break: while문을 강제로 빠져나가고 싶을 때
```

```
continue: while문을 빠져 나가지 않고 맨 처음 조건문으로 돌아가고 싶을 때
```

{% endhint %}

```python
# break를 이용한 탈출
box = 10

while True:
    print("상자를 구매합니다")
    box -= 1
    if not box:
        print("상자가 0개라 더 이상 구매할 수 없습니다.")
        break

# continue를 이용한 홀수만 출력
num = 0

while num < 10:
    num += 1
    if num % 2 == 0:
        continue
    print(num)
```
