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

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.

主站蜘蛛池模板: 荆门市| 定州市| 六安市| 仙游县| 红原县| 临沂市| 新绛县| 巫溪县| 蒙阴县| 石狮市| 怀柔区| 孟村| 胶州市| 阳城县| 林芝县| 会东县| 沂南县| 洞口县| 屯门区| 铁力市| 宁阳县| 富平县| 夏河县| 白沙| 海宁市| 弥渡县| 清水县| 东乌珠穆沁旗| 普安县| 千阳县| 温宿县| 大埔区| 江口县| 三穗县| 靖安县| 郧西县| 游戏| 兴隆县| 会东县| 蓬莱市| 荥阳市|