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

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.

主站蜘蛛池模板: 佳木斯市| 喀喇沁旗| 宁城县| 大理市| 兴文县| 北票市| 嘉善县| 洪湖市| 金坛市| 札达县| 军事| 和龙市| 罗田县| 都匀市| 盐边县| 威远县| 黄平县| 清水县| 龙胜| 青河县| 大埔区| 峨眉山市| 武宁县| 明水县| 岢岚县| 谢通门县| 通城县| 无为县| 武夷山市| 封开县| 盐城市| 阿瓦提县| 聊城市| 佳木斯市| 华阴市| 桐乡市| 安岳县| 尼玛县| 通化市| 湘乡市| 子长县|