05 循环

while

1、while
#while 循环
#死循环打印
while True:
    print("人生苦短,我用python ")
#2. while后加入条件   
  while 1>0 and 2 > 0:
    print("人生苦短,我用python ")
#3. while数字相加   
"""
count = 1 
value = count  +1 
print (value)
"""
count = 0
while True:
    print("count")
    count += 1
    
#5 通过循环打印:1,2,3....10
count =1 
while count <= 10:
    print(count)
    count +=1
#7 关键字: break     终止当前循环
while True:
    print("count")
    break  #终止当前循环
print("结束")    
    
2、while -break-continue
#7 关键字: break     终止当前循环
count = 1 
while True:
    print("count")
    count +=1
    if count  ==10:
	    break  #终止当前循环
print("结束")   

#while 嵌套
while True:
    print("hello")
    while True:
        print("666")
        break  #终止当前循环
    break #终止外层循环

#8 关键字 continue 本次循环如果遇到continue,则不在继续往下走,而是回到while条件位置
count = 1 
while count <=10:
    if count == 8:
        count += 1
    	continue
     print(count)
    count += 1

3、while else-了解即可
count = 1 
while count <=10:
    if count == 8:
        count += 1
    	continue
     print(count)
    count += 1
else:  #不再满足while后的条件时,触发,或者条件= False时
    print("ELSE代码块执行")

for

for
name = 'alex'
for item in name:
    print(item)
print(item)     #此时打印item最后一次循环的值:x
for+break
name = 'alex'
for item in name:
    print(item)
    break
    print('123')  #break后面的语句永远不会执行
for+continue
name = 'alex'
for item in name:
    print(item)
    continue  # continue 后面的语句永远不会执行
    print('123')
posted @ 2024-09-25 20:34  jhchena  阅读(2)  评论(0编辑  收藏  举报