1.while 循环
while是计算机的一种基本循环模式。当满足条件时进入循环,进入循环后,当条件不满足时,跳出循环。while语句的一般表达式为:while(表达式){循环体}。
循环的作用就是让 指定的代码 重复的执行
while 循环最常用的应用场景就是 让执行的代码 按照 指定的次数 重复 执行
基本语法
![](https://upload-images.jianshu.io/upload_images/25631092-51f6d47629329aa3.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
死循环
由于程序员的原因,忘记 在循环内部 修改循环的判断条件,导致循环持续执行,程序无法终止!
循环计算
打印小星星
- 在控制台连续输出五行 * ,每一行星号的数量依次递增
![](https://upload-images.jianshu.io/upload_images/25631092-019cacacac72f1d1.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
- 使用字符串 * 打印
![](https://upload-images.jianshu.io/upload_images/25631092-f710ea90360c844e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
循环打印小星星
假设 Python 没有提供 字符串的 * 操作 拼接字符串
需求
- 在控制台连续输出五行 * ,每一行星号的数量依次递增
![](https://upload-images.jianshu.io/upload_images/25631092-23d4c54138eee8eb.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
开发步骤
- 完成 5 行内容的简单输出
- 分析每行内部的 * 应该如何处理?
- 每行显示的星星和当前所在的行数是一致的
- 嵌套一个小的循环,专门处理每一行中 列 的星星显示
![](https://upload-images.jianshu.io/upload_images/25631092-f9fc694e916fb2b5.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
九九乘法表
需求 输出 九九乘法表,格式如下:
![](https://upload-images.jianshu.io/upload_images/25631092-45a297eb9eccbf9d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
开发步骤
-
打印 9 行小星星
-
将每一个 * 替换成对应的行与列相乘
总结:
写while循环,先写限制条件
![](https://upload-images.jianshu.io/upload_images/25631092-be1b81d86695cd06.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
2.for遍历
在计算机科学里,遍历的一种,指的是按照某种规则,不重复地访问某个对象的所有内容的过程。
![](https://upload-images.jianshu.io/upload_images/25631092-4fd1639fb6a9e950.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
语法
![](https://upload-images.jianshu.io/upload_images/25631092-b0b433f9f2e741f1.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
for i in range(10)
![](https://upload-images.jianshu.io/upload_images/25631092-f716a246368e01e5.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
range
range() 函数可创建一个整数列表,一般用在 for 循环中。
函数语法:
![](https://upload-images.jianshu.io/upload_images/25631092-ac46a60f19c7585e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
参数说明:
![](https://upload-images.jianshu.io/upload_images/25631092-101f1db28cf5f284.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![](https://upload-images.jianshu.io/upload_images/25631092-e5def2465cb844fc.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
for遍历可迭代对象
for可以迭代所有可迭代对象(字符串、列表、元组、集合、字典)
![](https://upload-images.jianshu.io/upload_images/25631092-6c6639c708a06e0e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
案例:用 for 迭代获取所有水仙花数
![](https://upload-images.jianshu.io/upload_images/25631092-2f8feda90953b799.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
案例:用for循环实现九九乘法表
![](https://upload-images.jianshu.io/upload_images/25631092-c6fa907259fd2b7b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
3.break 和 continue
break 和 continue 是专门在循环中使用的关键字
![](https://upload-images.jianshu.io/upload_images/25631092-8fab21a6a58f858d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
break 和 continue 只针对 当前所在循环 有效
break
从内部中断循环
- 在循环过程中,如果 某一个条件满足后,不再希望 循环继续执行,可以使用 break 退出循环
break 只针对当前所在循环有效
案例:登录账号
循环一直输入账号密码,输入成功之后跳出循环
![](https://upload-images.jianshu.io/upload_images/25631092-32a5485fab558639.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
continue
![](https://upload-images.jianshu.io/upload_images/25631092-a60fec425c8d6ace.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![](https://upload-images.jianshu.io/upload_images/25631092-30baf84f04aa828e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![](https://upload-images.jianshu.io/upload_images/25631092-8bf4416e248510a3.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
else 语句
else 可以在while和for循环中运用,else和while或for搭配使用,不再是条件判断的含义。而是与while与for组成了一个新的语句。正常结束就执行,不正常就是就不执行。
else与while一起使用
![](https://upload-images.jianshu.io/upload_images/25631092-9f1cf004ab047b56.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
else与for一起使用
![](https://upload-images.jianshu.io/upload_images/25631092-821bf5182b21674f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
@拓展
三元表达式
程序设计中,往往有一些巧妙的程序语言,能够使得程序看起来简练干净:
![](https://upload-images.jianshu.io/upload_images/25631092-932378910b0cf8df.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)