官术网_书友最值得收藏!

Breaking out of loops

Conditional statements can be used to break out of a loop execution (for loop and while loop). When a specific condition is met, an if statement can be used to break out of a loop:

i = 0 
while True:
print("The value of i is ", i)
i += 1
if i > 100:
break

In the preceding example, the while loop is executed in an infinite loop. The value of i is incremented and printed on the screen. The program breaks out of the while loop when the value of i is greater than 100 and the value of i is printed from 1 to 100.

主站蜘蛛池模板: 卓资县| 余江县| 伊通| 兴仁县| 兴化市| 郴州市| 潢川县| 屏边| 庆元县| 昌平区| 乌兰察布市| 巨野县| 上思县| 乐都县| 黎城县| 东至县| 宣恩县| 高雄市| 东乡族自治县| 威海市| 河津市| 宝鸡市| 定结县| 南皮县| 延津县| 阳高县| 永善县| 奉化市| 黄平县| 中西区| 庆城县| 浦县| 诸暨市| 抚顺市| 丹凤县| 灵川县| 文成县| 裕民县| 南昌县| 雅安市| 平泉县|