Lesson 3-2 语句:循环语句
3.2 循环语句
3.2.1 while 循环语句
--- while 语句包含:关键字while、条件、冒号、while子句(代码块)。
--- 执行while 循环,首先判断条件是否为真,如果为假,结束while循环,继续程序中后面的语句,如果为真,执行while子句的代码块,执行完后调回到while循环开始的地方,重新判断条件是否为真,如果为真,执行while子句的代码块,一遍一遍的循环执行,直至条件为假。
a = 1
while a < 10 :
print(a)
a = a +1 print('Hello world!') 结果:
/usr/bin/python3.7 /home/jlu/Projects/Python/untitled/2019-4-21/循环语句.py
1
2
3
4
5
6
7
8
9
Hello world! Process finished with exit code 0
&、注意while 语句和if 语句的区别,当条件为真时,while语句执行完一遍后会返回开始点,if 语句执行完一遍后不返回,继续往下执行。
3.2.2 for 循环语句
--- for 语句包含:for 关键字、一个变量名、in关键字、可迭代对象、冒号、for子句(代码块)。
--- for 语句含义:执行迭代(遍历)可迭代对象次数的for子句代码块。
lis = [1, 2, 3, 4, 5]
for i in lis:
print(i) total = 0
for num in range(101):
total = total + num
print(total) 结果:
/usr/bin/python3.7 /home/jlu/Projects/Python/untitled/2019-4-21/循环语句.py
1
2
3
4
5
5050 Process finished with exit code 0
3.2.3 break 语句
--- break 语句只包含break 关键字,通常放在if 语句的代码块中使用,用于满足一定条件时,立即结束当前迭代,并提前结束循环。
total = 0
for num in range(101):
total = total + num
if total > 2000 :
break print(num,total) 结果:
/usr/bin/python3.7 /home/jlu/Projects/Python/untitled/2019-4-21/循环语句.py
63 2016 Process finished with exit code 0
total = 0
num = 1
while num < 101 :
total = total + num
num = num +1
if total > 2000 :
break print(num,total) 结果:
/usr/bin/python3.7 /home/jlu/Projects/Python/untitled/2019-4-21/循环语句.py
64 2016 Process finished with exit code 0
3.2.4 continue 语句
--- continue 语句只包含continue 关键字,它结束当前迭代,并跳回到迭代开头处,继续进行循环条件判断,条件为真时继续进入循环。
num = 1
while num :
num = num * num + 1
print(str(num) + '-while')
if num < 100 :
continue
print(str(num) + '-continue')
if num > 1000 :
break
print(str(num) + '-break')
print(str(num) + '-end') 结果:
/usr/bin/python3.7 /home/jlu/Projects/Python/untitled/2019-4-21/循环语句.py
2-while
5-while
26-while
677-while
677-continue
677-break
458330-while
458330-continue
458330-end Process finished with exit code 0
Lesson 3-2 语句:循环语句的更多相关文章
- python学习第四讲,python基础语法之判断语句,循环语句
目录 python学习第四讲,python基础语法之判断语句,选择语句,循环语句 一丶判断语句 if 1.if 语法 2. if else 语法 3. if 进阶 if elif else 二丶运算符 ...
- 【Python】-NO.99.Note.4.Python -【Python3 条件语句 循环语句】
1.0.0 Summary Tittle:[Python]-NO.99.Note.4.Python -[Python3 条件语句 循环语句] Style:Python Series:Python Si ...
- delphi if 语句循环语句
if语句------------------------------------------------------------------------------------------------ ...
- [转]PB 基本语句 循环语句
PB 基本语句一.赋值语句赋值语句用于给变量.对象属性赋值,这是应用程序中使用最频繁的语句,其语法格式为:variablename = expression_r其中:⑴variablename是变量名 ...
- python 分支语句 循环语句
分支语句 #if-else if a > b: print('aaa') else: print('bbb') #if-elif-else if a > b: print('a>b' ...
- JavaScript基本语法 -- 条件语句 & 循环语句
条件语句 条件语句(Conditional statement)是JavaScript里面的基本结构之一,程序根据表达式的真假决定执行或者跳过某个分支,于是,条件语句有时候也可以称为"分支语 ...
- python - 条件语句/循环语句/迭代器
条件测试:if 条件表达式python 的比较操作 所有的python对象都支持比较操作 可用于测试相等性.相对大小等 如果是复合对象,pyt ...
- java基础 流程控制和条件语句,循环语句
顺序结构 程序的顺序结构: 如果代码里没有流程控制,程序是按照书写的格式从上而下一行一行执行的, 一条语句执行完之后继续执行下一条语句,中间没有判断和跳转,直到程序的结束. if语句 if语句使用bo ...
- js基础(条件语句 循环语句)
条件语句 if语句块的语法形式如下: //只有两种情况下if(条件){要执行的语句块;}else{要执行的语句块;} //多种情况下if(条件){要执行的语句块;}else if(条件){要执行的语句 ...
- c#基础语句——循环语句(for、while、foreach)
循环类型:for.while.foreach 循环四要素:初始条件-->循环条件-->循环体-->状态改变 1.for 格式: for(初始条件:循环条件:状态改变) {循环体(br ...
随机推荐
- webpack基本用法
1. 压缩代码(production模式) optimization: { minimizer: [new UglifyJsPlugin({ cache: true, parallel: true, ...
- Jenkins+Git+Maven搭建自动化构建平台
http://blog.csdn.net/xlgen157387/article/details/50353317
- python学习日记(继承和多态)
继承 在OOP程序设计中,当我们定义一个class的时候,可以从某个现有的class继承,新的class称为子类(Subclass),而被继承的class称为基类.父类或超类(Base class.S ...
- [BJOI2019]删数(线段树)
[BJOI2019]删数(线段树) 题面 洛谷 题解 按照值域我们把每个数的出现次数画成一根根的柱子,然后把柱子向左推导,\([1,n]\)中未被覆盖的区间长度就是答案. 于是问题变成了单点修改值,即 ...
- Python中布尔值是False的所有值
在python中以下都是False:为0的,空集合,空字符串,空值None >>> bool(0) False >>> bool(-0) False >> ...
- 第四十六篇--解析和保存xml文件
新建assets资源文件夹,右键app --> new --> Folder --> Assets Folder,将info.xml放入此文件夹下面. info.xml <?x ...
- (二叉树 BFS) leetcode993. Cousins in Binary Tree
In a binary tree, the root node is at depth 0, and children of each depth knode are at depth k+1. Tw ...
- CTF--web
https://adworld.xctf.org.cn/task/task_list?type=web&number=3&grade=0 1.view source 查看源代码 1.鼠 ...
- (二) V4L2引入(含浅析UVC)
title: V4L2引入(含浅析UVC) date: 2019/4/23 19:00:00 toc: true --- V4L2引入(含浅析UVC) 基本框架 V4L2全名是video for li ...
- SpringMVC运行流程
Spring工作流程描述 1. 用户向服务器发送请求,请求被Spring 前端控制Servelt DispatcherServlet捕获: 2. DispatcherServl ...