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

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.

主站蜘蛛池模板: 疏附县| 绵阳市| 青田县| 蓝山县| 曲松县| 东乌珠穆沁旗| 象州县| 邻水| 兴宁市| 嵊泗县| 剑川县| 门源| 登封市| 彭山县| 荣昌县| 通州区| 宜宾县| 镇沅| 盐城市| 平塘县| 灌南县| 彩票| 兴安盟| 桐梓县| 长岭县| 班戈县| 临海市| 嘉禾县| 石柱| 滦平县| 普兰店市| 平阴县| 宜春市| 安庆市| 辽宁省| 彭泽县| 萝北县| 新干县| 庆云县| 会昌县| 加查县|