Posts [Python] Pass, Continue, Break
Post
Cancel

[Python] Pass, Continue, Break

Python

- pass, continue, break 차이점

  • pass : 실행할 코드가 없는 것으로 다음 행동을 계속해서 진행합니다.
  • continue : 바로 다음 순번의 loop를 수행합니다.
  • break : 반복문을 멈추고 loop 밖으로 나가도록합니다.

1. pass 예시

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
## 실행
for i in range(10):
    if i % 2 == 0:
        pass
        print(i)    
    else:
        print(i)
print("Done")

## 결과
0
1
2
3
4
5
6
7
8
9
Done

2. continue 예시

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
## 실행
for i in range(10):
    if i % 2 == 0:
        continue
        print(i)    
    print(i)
print("Done")

## 예시
1
3
5
7
9
Done

3. break 예시

1
2
3
4
5
6
7
8
9
10
11
## 실행
for i in range(10):
    if i % 2 == 0:
        break
        print(i)    
    else:
        print(i)
print("Done")

## 예시
Done
저자는 이 저작물에 대해 CC BY-NC 4.0 라이선스를 가집니다.