728x90
728x90

while() - 지정 조건이 참일 때까지 블록 구문을 불러온다

 

i=1
while i<5:
    print("{}번 반복".format(i))
    i=i+1
    #i++
i=1

while i<10:
    print("{} x {} = {}".format(2,i,2*i))
    i+=1
i=1

while True:
    print(i, "번째 출력")
    if i==10:
        break
    i+=1
i=0

while i<10:
    i+=1
    if i==3:
        continue
    print(i, "번쨰 출력")

 

 

컬렉션데이터 반복

Attendance=["김순희", "김영철", "최민규"]
index=0

while index<len(Attendance):
    print(Attendance[index])
    index += 1

 

 

for while() : ~하는 동안

Attendance=["김순희", "김영철", "최민규"]

for name in Attendance:
    print(name)

문자열은 반복문을 통해서 한글자씩 출력할 수 있다

for x in "python":
    print(x)
#p
#y
#t
#h
#o
#n

break : 반복문을 중단, continue : 한단계만 생략하고 다음 반복문 진행

Attendance=["김순희", "김영철", "최민규", "민지"]

for name in Attendance:
    print(name)
    if name=="최민규":
        break

Attendance=["김순희", "김영철", "최민규", "민지"]

for name in Attendance:
    
    if name=="최민규":
        continue #제외한 나머지가 출력
    print(name)

 

range

for x in range(2,6):
    print(x)
for x in range(1, 15, 2):
    print(x) #공차설정

 

for x in range(3):
    print(x) 
else :
    print("반복문 종료")

for x in range(3):
    if x==2:
        break
    print(x) 

else :
    print("반복문 종료") #반복문 루프가 마지막까지 확인하는 용도로도 쓴다

 

2중 for문

for x in range(2, 10):
    print("%d단" %x)
    for y in range(1,10):
        print("{}x{}={}".format(x,y,y*x))
728x90
728x90
블로그 이미지

coding-restaurant

코딩 맛집에 방문해주셔서 감사합니다.

,

v