1. python-while循环语句
-
while循环详细图
-
while语句:
在某条件下循环执行一段代码,即重复相同的任务
-
while语法格式:
while <表达式>: <代码块>
-
while语法示例:
-
示例1:当条件满足时停止循环
count = 0 while count < 5: print(count) count += 1
-
示例2:死循环
count = 0 while True: print(count) count += 1
-
-
continue与break语句
continue 当满足条件时,跳出本次循环
break 当满足条件时,跳出所有循环
-
示例1:continue
# 示例1:continue for n in range(1,6): if n == 3: continue else: print(n)
-
示例2:break
for n in range(1,6): if n == 3: break else: print(n)
-
注释
只有在for、while循环语句中才有效
-
2. 案例
2.1 案例:while-基础使用
#!/usr/bin/env python3
# _*_ coding: utf-8 _*_
# Author:shichao
# File: .py
# 满足条件就退出
a = 0
while a <= 10:
print(a)
a += 1
# 死循环
count = 0
while True:
print(count)
count += 1
2.2 案例:while-使用continue和break语句
#!/usr/bin/env python3
# _*_ coding: utf-8 _*_
# Author:shichao
# File: .py
# 示例1:continue
for n in range(1,6):
if n == 3:
continue # continue跳出本次循环
else:
print(n)
# 使用break语句
for i in range(1,6):
if i == 3:
break # break跳出所有循环
else:
print(i)
# 使用while语句
i = 0
while i <=10:
if i == 3:
print("循环到3了,跳出本次循环")
i += 1
continue
else:
print(i)
i += 1
标签:语句,count,示例,python,break,while,continue
From: https://www.cnblogs.com/scajy/p/17005414.html