本文概述
- 例子1
- 例子2
- 示例3:带有while循环的break语句
- 例子3
该中断通常用于需要为给定条件中断循环的情况下。
中断的语法如下。
#loop statements
break;
例子1
list =[1, 2, 3, 4]
count = 1;
for i in list:
if i == 4:
print("item matched")
count = count + 1;
break
print("found at", count, "location");
输出
item matched
found at 2 location
例子2
str = "python"
for i in str:
if i == 'o':
break
print(i);
输出
p
y
t
h
示例3:带有while循环的break语句
i = 0;
while 1:
print(i, " ", end=""), i=i+1;
if i == 10:
break;
print("came out of while loop");
输出
0123456789came out of while loop
例子3
n=2
while 1:
i=1;
while i<
=10:
print("%d X %d = %d\n"%(n, i, n*i));
i = i+1;
choice = int(input("Do you want to continue printing the table, press 0 for no?"))
if choice == 0:
break;
n=n+1
【Python break语句用法示例】输出
2 X 1 = 22 X 2 = 42 X 3 = 62 X 4 = 82 X 5 = 102 X 6 = 122 X 7 = 142 X 8 = 162 X 9 = 182 X 10 = 20Do you want to continue printing the table, press 0 for no?13 X 1 = 33 X 2 = 63 X 3 = 93 X 4 = 123 X 5 = 153 X 6 = 183 X 7 = 213 X 8 = 243 X 9 = 273 X 10 = 30Do you want to continue printing the table, press 0 for no?0
推荐阅读
- Python注释用法
- Python应用程序示例
- SciPy子程序包介绍和解析
- 10款最佳Windows 10 UI自定义工具下载推荐合集(哪个最好())
- Windows的10款最佳Epub阅读器下载推荐合集(哪个最好用())
- 最好的房地产管理软件下载推荐合集(哪款更好用())
- Android ImageView属性
- flutter 项目中打印原生安卓的log信息
- Unity-使用UnityRemote调试手机Android Studio打印日志