码迷,mamicode.com
首页 > 其他好文 > 详细

循环语句

时间:2019-05-31 23:52:11      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:循环   bre   style   while   ==   结束   跳过   font   你好   

# 1.循环打印"人生苦短,及时行乐。"
while True:
print(‘人生苦短,及时行乐‘)

# 2.while后加入条件
while 1 > 0:
print(‘人生苦短,不如去死‘)

# 3.数字相加
count = 1
count = count + 1
print(count)

# 4.通过循环,让count每次循环都+1
count = 1
while True:
print(count)
count = count + 1 # 将变量count重新赋值之后再输出

# 5.通过循环,打印1,2,3....10
count = 1
while count <= 10:
print(count)
count = count + 1

# 6.通过循环,打印1,2,3,4,5,6,8,9,10
# ①先打印1到6,再打印8到10
count = 1
while count <= 6:
print(count)
count = count + 1
count = 8
while count <= 10:
print(count)
count = count + 1

# ②在while中嵌套一个if实现
count = 1
while count <= 10:
if count != 7:
print(count)
count = count + 1

# ③.引入pass
count = 1
while count <= 10:
if count == 7:
pass # 当执行到count = 7时,直接跳过
else:
print(count)
count = count + 1 # 变量count是在while的下一级

# 7.关键字break
while True:
print(666)
break # 注:终止当前循环
print(‘结束‘)

# 练习1:通过break实现1~10
count = 1
while True:
print(count)
count = count + 1
if count == 11:
break
print(‘结束‘)

# break始终终止当前循环
while True:
print(‘你好!‘)
while True:
print(666)
break
break

# 关键字continue
count = 1
while True:
print(count)
continue # 本次循环不再继续往下走,回到while条件位置
count = count + 1
# 练习1~10,不打印7
count = 1
while count <= 10:
if count == 7:
count = count + 1
continue
print(count)
count = count + 1

# 9.while else
count = 1
while count <= 10:
print(count)
count = count + 1
else: # 不再满足while条件或条件=False时执行此步骤
print(‘ELSE代码块‘)

循环语句

标签:循环   bre   style   while   ==   结束   跳过   font   你好   

原文地址:https://www.cnblogs.com/zyg-ayy/p/10957853.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!