本人CSDN博客专栏:https://blog.csdn.net/yty_7
Github地址:https://github.com/yot777/Python-Primary-Learning
6.3 循环结构——for循环
Python的for循环可以遍历任何序列的项目,如一个列表或者一个字符串。
for循环的一般格式如下:
for
else:
循环语句可以加入else、break、 continue、pass关键字,作用如下:
1、else语句
如果在穷尽列表后(以for循环)或条件变为假(以while循环)循环终止时被执行
2、break语句
用来跳出for和while的整个循环体。一旦跳出,循环中任何剩下的程序块(包括else)将不执行。
3、continue语句
用来跳过当前循环块中的剩余语句,然后继续进行下一轮循环。
4、pass语句,继续当前循环,相当于没有任何作用
Python——for循环和break、else举例1
edibles = ["ham","eggs","spam","nuts"]
for food in edibles:
#edibles列表遍历到spam的时候,执行print("No more spam please!")语句,然后跳出循环
if food == "spam":
print("No more spam please!")
break
print("Great, delicious " + food)
else:
print("I am so glad: No spam!")
print("Finally, I finished stuffing myself.")
运行结果:
Great, delicious ham
Great, delicious eggs
#edibles列表遍历到spam的时候,执行print("No more spam please!")语句,然后跳出循环
No more spam please!
Finally, I finished stuffing myself.
Python——for循环和break、else举例2
edibles = ["ham","eggs","nuts"]
for food in edibles:
#列表edibles没有spam
if food == "spam":
print("No more spam please!")
break
print("Great, delicious " + food)
#列表edibles没有spam,因此执行else的print("I am so glad: No spam!")语句
else:
print("I am so glad: No spam!")
print("Finally, I finished stuffing myself.")
运行结果:
Great, delicious ham
Great, delicious eggs
Great, delicious nuts
#列表edibles没有spam,因此执行else的print("I am so glad: No spam!")语句
I am so glad: No spam!
Finally, I finished stuffing myself.
Java ——for循环和break 举例
public class Test4 {
public static void main(String[] args) {
String[] edibles = {"ham","eggs","nuts"};
int i=0;
for (String food : edibles){
if(food == "spam"){
System.out.println("No more spam please!");
break;
}
System.out.println("Great, delicious " + food);
i=i+1;
} //Java的for循环没有最后的else,如果要实现Python里for-else功能需要自己写逻辑
if(i==edibles.length){
System.out.println("I am so glad: No spam!");
}
System.out.println("Finally, I finished stuffing myself.");
}
}
运行结果:
Great, delicious ham
Great, delicious eggs
Great, delicious nuts
I am so glad: No spam!
Finally, I finished stuffing myself.
Python——for循环和continue举例
edibles = ["ham","eggs","spam","nuts"]
for food in edibles:
#edibles列表遍历到spam的时候,跳过当前循环块的剩余语句,然后继续进行下一轮循环
if food == "spam":
continue
print("Great, delicious " + food)
print("Finally, I finished stuffing myself.")
运行结果:
Great, delicious ham
Great, delicious eggs
#edibles列表遍历到spam的时候,跳过当前循环块的剩余语句,然后继续进行下一轮循环
Great, delicious nuts
Finally, I finished stuffing myself.
Python——for循环和pass举例
edibles = ["ham","eggs","spam","nuts"]
for food in edibles:
#edibles列表遍历到spam的时候,pass语句不起任何作用,仍然print("Great, delicious " + food)
if food == "spam":
pass
print("Great, delicious " + food)
print("Finally, I finished stuffing myself.")
运行结果:
Great, delicious ham
Great, delicious eggs
#edibles列表遍历到spam的时候,pass语句不起任何作用,仍然print("Great, delicious " + food)
Great, delicious spam
Great, delicious nuts
Finally, I finished stuffing myself.
参考教程:
廖雪峰的Python教程
https://www.liaoxuefeng.com/wiki/1016959663602400
廖雪峰的Java教程
https://www.liaoxuefeng.com/wiki/1252599548343744
Python3 教程 | 菜鸟教程
https://www.runoob.com/python3/
如果您觉得本篇本章对您有所帮助,欢迎关注、评论、点赞!Github欢迎您的Follow、Star!