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

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.

主站蜘蛛池模板: 雅安市| 墨竹工卡县| 通化县| 南郑县| 滦平县| 酉阳| 左云县| 贵阳市| 区。| 左权县| 尼玛县| 红桥区| 抚州市| 贵德县| 麻栗坡县| 华池县| 东丽区| 和平区| 灵川县| 孟津县| 陆河县| 通城县| 沁阳市| 根河市| 香格里拉县| 石首市| 肃南| 雷州市| 长宁区| 包头市| 鲁甸县| 诸城市| 大同市| 固安县| 天峨县| 宁武县| 锦屏县| 松潘县| 黎川县| 洛隆县| 隆尧县|