티스토리 뷰

0. 반복문(Loop)

반복문은 프로그램 내에서 동일한 내용을 반복해서 실행 할 때 사용한다.

1) 반복문의 일반적인 형태

2) 반복문의 종류

구분 설명
while 명령문을 실행하기 전 반복 조건문의 결과를 구하고, 참 일 동안 명령문 또는 그룹을 반복한다.
for 명령문을 실행하기 전 반복 조건문의 결과를 구하고, 참 일 동안 명령문 또는 그룹을 반복한다.
중첩된 반복문 반복문 내부에 하나 이상의 반복문이 존재하는 반복문의 형태이다. 

3) 반복문의 제어

구분 설명
break 반복문을 종료하고 반복문 바로 다음 명령문을 실행할때 사용한다.
continue 반복문의 나머지 부분을 건너 뛰고, 반복하기 전 상태에서 반복 조건문의 결과를 구할때 사용한다.
pass 반복문의 나머지 코드를 실행하지 않고 건너뛸 경우에 사용한다. 

1. while 문 

while 문은 반복 조건문이 참(true)값일 경우 명령문 또는 명령문그룹을 반복하여 실행한다. 반복 조건문의 값이 거짓(false)이 되면 다음 문장으로 넘어갈 수 있다.
주의할 내용은 반복 조건문의 결과가 거짓이 되지 않으면 끝없이 프로그램을 실행하는 무한 루프에 빠지게 된다. 

1) 기본적인 while 문 의 구조

while 조건문:
	실행할 문장
    실행할 문장
    ....

while 문이 완료된 후 실행할 문장
# 07_01_01_PythonWhileStatement

condition = 0

while(condition < 10):
    print("The Condition is :", condition)
    condition += 1

print("End Program")

# The Condition is : 0
# The Condition is : 1
# The Condition is : 2
# The Condition is : 3
# The Condition is : 4
# The Condition is : 5
# The Condition is : 6
# The Condition is : 7
# The Condition is : 8
# The Condition is : 9
# End Program

2) 무한루프

무한루프란 프로그램을 끝없이 반복하여 실하고자 할 경우에 사용한다. 

# 07_01_02_PythonInfiniteWhile

while 1:
    newString = input("Please enter a letter: ")
    print("The character you entered is ",  newString )

3) while ~ else 문

# 07_01_03_PythonWhileElse

condition = 0

while(condition < 10):
    print("The Condition is :", condition)
    condition += 1
else:
    print("While 문 실행 후 추가 실행")

print("End Program")

# The Condition is : 0
# The Condition is : 1
# The Condition is : 2
# The Condition is : 3
# The Condition is : 4
# The Condition is : 5
# The Condition is : 6
# The Condition is : 7
# The Condition is : 8
# The Condition is : 9
# While 문 실행 후 추가 실행
# End Program

2. for 문

for문은 반복 조건에 시퀀스나 값를 추가하여 소진될때까지 명령문 또는 명령문 그룹을 반복한다. 

1) 기본적인 for문의 구조 ( 시퀀스 구조 )

for 아이템 in 시퀀스:
	실행할 문장
	....
    
for문 이후의 실행 문장
# 07_02_01_PythonForStatement

indexSequence = ["One", "Two", "Three", "Four"]

for index in indexSequence:
    print(index)

print("End Program")

# One
# Two
# Three
# Four
# End Program

2) 시퀀스의 인덱스를 이용한  for문의 구조

시퀀스를  Index화 하여 for 문을 사용해야 하는 경우가 많이 발생한다 이를때 사용하는 방법이다. 

# 07_02_02_PythonForSequenceIndex

indexSequence = ["One", "Two", "Three", "Four"]

for index in range(len(indexSequence)):
    print("This index is : ", index, ", This Sequence Value is : " , indexSequence[index])

# This index is :  0 , This Sequence Value is :  One
# This index is :  1 , This Sequence Value is :  Two
# This index is :  2 , This Sequence Value is :  Three
# This index is :  3 , This Sequence Value is :  Four


for index in range(10, 15):
    print("This index is : ", index)
print("End Program")

# This index is :  10
# This index is :  11
# This index is :  12
# This index is :  13
# This index is :  14

 

3) for ~ else 문

# 07_02_03_PythonForElse

for index in range(1, 10):
    print(index)
else :
    print("For 문 실행 후 추가 실행")

print("End Program")

# 1
# 2
# 3
# 4
# 5
# 6
# 7
# 8
# 9
# For 문 실행 후 추가 실행
# End Program

4) 중첩된 반복문(Nested Loop Statement)

반복문 내에 반복문을 사용하는 방식이다.

# 07_02_04_PythonNestedFor

for indexOut in range(1, 3):
    print("Index Out Loop : ", indexOut)
    for indexIn in range(10, 13):
        print("  Index Out : ", indexOut, "Index In : ", indexIn )

# Index Out Loop :  1
#   Index Out :  1 Index In :  10
#   Index Out :  1 Index In :  11
#   Index Out :  1 Index In :  12
# Index Out Loop :  2
#   Index Out :  2 Index In :  10
#   Index Out :  2 Index In :  11
#   Index Out :  2 Index In :  12

 

5) break문

break문은 반복문의 내부에 존재할 수 있으며, 반복문을 종료하고 다음 문을 실행할 때 사용한다. 

# 07_02_05_PythonForBreak

for index in range(1, 10):
    print(index)
    if index == 5:
        break;

print("End Program")

# 1
# 2
# 3
# 4
# 5
# End Program

 

6) continue 문

continue 문 이후에 반복하는 블럭의 명령문을 모두 취소하고 처음으로 이동하여 반복문을 실행할 때 사용한다.

# 07_02_06_PythonForContinue

for index in range(1, 10):
    print("Continue before statement : ", index)
    if index == 5:
        continue
    print("Continue after statement : ", index)
print("End Program")

# Continue before statement :  1
# Continue after statement :  1
# Continue before statement :  2
# Continue after statement :  2
# Continue before statement :  3
# Continue after statement :  3
# Continue before statement :  4
# Continue after statement :  4
# Continue before statement :  5
# Continue before statement :  6
# Continue after statement :  6
# Continue before statement :  7
# Continue after statement :  7
# Continue before statement :  8
# Continue after statement :  8
# Continue before statement :  9
# Continue after statement :  9
# End Program

 

7) pass 문

pass문은 구문적으로 필요하지만 명령문이나 명령문 그룹을 실행하지 않고 싶을 때 사용한다.

일반적으로 오류 제어에서 많이 사용한다. 

# 07_02_07_PythonForPass

for index in range(1, 10):
    print("Pass before statement : ", index)
    if index == 5:
        pass
        print("Pass after statement : ", index)
print("End Program")

# Pass before statement :  1
# Pass before statement :  2
# Pass before statement :  3
# Pass before statement :  4
# Pass before statement :  5
# Pass after statement :  5
# Pass before statement :  6
# Pass before statement :  7
# Pass before statement :  8
# Pass before statement :  9
# End Program
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2024/05   »
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
글 보관함