博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python基础-循环语句while
阅读量:4473 次
发布时间:2019-06-08

本文共 774 字,大约阅读时间需要 2 分钟。

循环语句:while\for\嵌套

循环控制语句:break\continue

  break:跳出整个循环,不会再继续循环下去

  continue:跳出本次循环,继续下一次循环

 

while循环:

 

1 count = 02 while (count < 9):3     print("count=",count)4     count += 15 print("while循环结束")

 

结果:

count= 0

count= 1
count= 2
count= 3
count= 4
count= 5
count= 6
count= 7
count= 8
while循环结束

continue语句:

i = 1while i<10:    i+=1    if i%2>0: #如果i%2的值大于0,则跳出本次循环,后面的语句不会执行,直接继续下一次循环,        continue    print(i)

 

结果:

2

4
6
8
10

break语句:

i = 1while 1: #死循环    print(i)    i += 1    if i>10:#循环到i=11的时候,执行break,跳出整个while循环        break

 

结果:

1 2 3 4 5 6 7 8 9 10

 

else语句:

while-else 与 if-else一样

count = 0while count <5:    print(count,"小于5")    count = count + 1else:    print(count,"大于等于5")

 

结果:

0 小于5

1 小于5
2 小于5
3 小于5
4 小于5
5 大于等于5

 

转载于:https://www.cnblogs.com/R-bear/p/6954370.html

你可能感兴趣的文章
linux shell学习五
查看>>
微软中文MSDN上的一些文章链接
查看>>
复习知识点:GCD多线程
查看>>
Objective-C中的类目(Category),延展(Extension)
查看>>
「学习笔记」wqs二分/dp凸优化
查看>>
面试笔试
查看>>
小波去噪的基本知识
查看>>
Prime Path POJ - 3126
查看>>
程序员的书袋
查看>>
mysql 分库分表转
查看>>
Android开源项目发现---TextView,Button篇(持续更新)
查看>>
1059 C语言竞赛
查看>>
视频算法分类概述
查看>>
MSTAR GUI
查看>>
使用std::string 编辑器奇怪问题
查看>>
文件提交
查看>>
常见网络开发库
查看>>
tensorflow softmax_cross_entropy_with_logits函数
查看>>
SQL 常用脚本
查看>>
Android的Task和Activity相关
查看>>