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

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.

主站蜘蛛池模板: 额尔古纳市| 阿拉善盟| 徐闻县| 临猗县| 玛沁县| 商河县| 互助| 长岛县| 黔西| 满洲里市| 邢台县| 通州区| 新沂市| 杨浦区| 永城市| 榆社县| 新安县| 阿坝| 武功县| 两当县| 北川| 洛阳市| 湘阴县| 巧家县| 姚安县| 准格尔旗| 抚州市| 始兴县| 交城县| 阿克苏市| 化德县| 许昌县| 平昌县| 沙河市| 鹿泉市| 尼木县| 巩义市| 垫江县| 喜德县| 双流县| 简阳市|